Search code examples
installationinno-setuppascalscript

Replace placeholder in an installed text file with input entered by user in Inno Setup


As part of an Inno Setup built installer, I want to output the text field that the user enters into the installer to a text file.

So far I have the below:

[Code]
var
  PrimaryServerPage: TInputQueryWizardPage;
  PrimaryAddress: String;

procedure InitializeWizard;
begin
  PrimaryServerPage := CreateInputQueryPage(wpWelcome,
    'Primary Server Details', 'Where is you application installed?',
    'Please specify the IP address or hostname of your Primary Server, ' +
    'then click Next.');
  PrimaryServerPage.Add('Primary Server IP/Hostname:', false); 

  PrimaryAddress := PrimaryServerPage.Values[0];

  SaveStringToFile('c:\filename.txt', PrimaryAddress, True);
end;

However, when I run the installer and enter the field it does not output to the text file.

If I replace PrimaryServerPage.Values[0] with a number this is successfully output to the text file.

Can anyone help or offer suggestions on where I might be going wrong?

Also, following this I actually want to output this value into the middle of an existing text file, is this possible? For example here is the config file I wish to insert it into. Value to be added into ENTER VALUE HERE! Can this be added as a last step of the installation? The config file will not exist until after the install is complete?

###############################################################################
#
#    Configuration File.
#
###############################################################################

#
# This file is intended for advanced users. Please consult the documentation
# before modifying this file.
#
# NOTE: The hash (#) represents a comment.
#

#
# Define the name or IP address of the primary server.
# On secondary server installs, this value should be changed to point to the
# primary server.
#   Default: 127.0.0.1
#   Examples:  mainserver.localdomain.com, win2003, 1.2.3.4
#
# IMPORTANT: Please restart the Service" after
# changing this value.
#
ApplicationServer=ENTER VALUE HERE!

Work in progress, getting stuck with getting the text file output working (I think I may be misunderstanding the post on this) before I look at the replace although any guidance around that would be great as I'm sure my inexperience with Inno is going to catch me out there too.

[Code]
var
  PrimaryServerPage: TInputQueryWizardPage;
  PrimaryAddress: String;

   
function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if(CurPageID = wpWelcome) then
  begin

  PrimaryServerPage := CreateInputQueryPage(wpWelcome,
    'Application Server Details', 'Where is your app installed?',
    'Please specify the IP address or hostname of your Application Server, ' +
    'then click Next.');
  PrimaryServerPage.Add('Primary Server IP/Hostname:', false); 

  PrimaryAddress := PrimaryServerPage.Values[0];

  SaveStringToFile('c:\filename.txt', PrimaryAddress, True);
  end;
 
  Result :=True;
end;

Solution

  • Combining answers to these two questions:

    you will get a code like:

    var
      PrimaryServerPage: TInputQueryWizardPage;
    
    function FileReplaceString(ReplaceString: string):boolean;
    var
      MyFile : TStrings;
      MyText : string;
    begin
      Log('Replacing in file');
      MyFile := TStringList.Create;
    
      try
        Result := true;
    
        try
          MyFile.LoadFromFile(ExpandConstant('{app}' + '\thefile.txt'));
          Log('File loaded');
          MyText := MyFile.Text;
    
          { Only save if text has been changed. }
          if StringChangeEx(MyText, 'REPLACE_WITH_IP', ReplaceString, True) > 0 then
          begin;
            Log('IP address inserted');
            MyFile.Text := MyText;
            MyFile.SaveToFile(ExpandConstant('{app}' + '\thefile.txt'));
            Log('File saved');
          end;
        except
          Result := false;
        end;
      finally
        MyFile.Free;
      end;
      
      Result := True;
    end;
    
    procedure InitializeWizard;
    begin
      PrimaryServerPage :=
        CreateInputQueryPage(
          wpWelcome, 'PaperCut Application Server Details', 'Where is PaperCut installed?',
          'Please specify the IP address or hostname of your ' +
            'Primary PaperCut Application Server, then click Next.');
      PrimaryServerPage.Add('Primary Application Server IP/Hostname:', False);
    end;   
    
    procedure CurStepChanged(CurStep: TSetupStep);
    begin
      if CurStep = ssPostInstall then
      begin
        Log('File installed, replacing IP address');
        FileReplaceString(PrimaryServerPage.Values[0]);
      end;
    end;
    

    To do the replacement earlier in the installation process, see also: