Inno Setup – Modify a Text File

Found this post on SO, Can I edit a file before installing with Inno Setup?, that suggests using a LoadStringFromFile(), StringChangeEx() and SaveStringToFile() to handle the processing.

So what LoadStringFromFile) does is load the entire text file in to a string.  If this is successful, then StringChange() does a string replace globally on the string, and then SaveStringToFile() writes the entire file back out.

Here is the version for our installer that replaces “$BaseName$” and “$Version$” with the appropriate values in our config files.

// Expands the configuration variables in the file to the right value based on the installation
// for the file and replaces the file.  Currently this only supports $BaseName$.
  procedure ExpandVariablesInFile(FileName: String);
  var
    data: String;
  begin  
    if LoadStringFromFile(FileName, data) then
    begin
      StringChangeEx(data, '$BaseName$', baseName, True);
      StringChangeEx(data, '$Version$', baseName, True);
      SaveStringToFile(FileName, data, False);      
    end; 
  end;
 
procedure DoPostInstall();
begin
  ExpandVariablesInFile(ExpandConstant('{app}\bin\CreateVdirs.bat'));
end;