skr
skr

Reputation: 1811

Inno Setup to match and replace exact strings in a file

I have a batch file wherein I set the java path as

For 32 bit

set JRE_HOME=%INSTALL_DIR%\java

and for 64 bit

set JRE_HOME=%INSTALL_DIR%\java_x64

these values are in multiple lines. I need to replace these lines as following

set JRE_HOME=%INSTALL_DIR%\java To set JRE_HOME=C:\Program Files (x86)\java

and

set JRE_HOME=%INSTALL_DIR%\java_x64 To set JRE_HOME=C:\Program Files\java

Problems with my code

[Code]

function FileReplaceString(const FileName, SearchString, ReplaceString: string): Boolean;
var
  MyFile : TStrings;
  MyText : string;
begin
  MyFile := TStringList.Create;

  try
    result := true;

    try
      MyFile.LoadFromFile(FileName);
      MyText := MyFile.Text;

      { Only save if text has been changed. }
      if StringChangeEx(MyText, SearchString, ReplaceString, True) > 0 then
      begin;
        MyFile.Text := MyText;
        MyFile.SaveToFile(FileName);
      end;
    except
      result := false;
    end;
  finally
    MyFile.Free;
  end;
end;

procedure CurStepChanged(CurStep: TSetupStep);
var
  Java32,Java64: string;
  JREVersion:integer;
begin
  if CurStep = ssDone then
  begin
    JREVersion := 32;

    if JREVersion = 32 then
    begin
      Java32 := ExpandConstant('{pf}') + '\java';
      if FileReplaceString(
           ExpandConstant('D:\authorized\Builds\Solo\custom.bat'),
           'set JRE_HOME=%INSTALL_DIR%\java',
           'set JRE_HOME=' + Java32) 
      then
        MsgBox('Java32 path has been set!', mbInformation, MB_OK)
      else  
        MsgBox('Java32 path has not been set!.', mbError, MB_OK)
    end;
  end;
end;

Upvotes: 1

Views: 935

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202474

Replace only the instances that end with a new line:

FileReplaceString(
  ExpandConstant('D:\authorized\Builds\Solo\custom.bat'),
  'set JRE_HOME=%INSTALL_DIR%\java'#13#10,
  'set JRE_HOME='+Java32+#13#10)

Upvotes: 1

Related Questions