我有一个批处理文件,其中我将java路径设置为
32位
set JRE_HOME=%INSTALL_DIR%\java
和64位
set JRE_HOME=%INSTALL_DIR%\java_x64
这些值在多行中。我需要按以下方式替换这些行
set JRE_HOME=%INSTALL_DIR%\java
收件人 set JRE_HOME=C:\Program Files (x86)\java
和
set JRE_HOME=%INSTALL_DIR%\java_x64
收件人 set JRE_HOME=C:\Program Files\java
我的代码有问题
set JRE_HOME=%INSTALL_DIR%\java
并将其替换为set JRE_HOME=C:\Program Files (x86)\java
时,它也匹配第二个字符串,即set JRE_HOME=%INSTALL_DIR%\java_x64
并像JRE_HOME=C:\Program Files (x86)\java_x64
一样替换set JRE_HOME=%INSTALL_DIR%\java
而不是set JRE_HOME=%INSTALL_DIR%\java_x64
[代码]
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;
答案 0 :(得分:1)
仅替换以新行结尾的实例:
FileReplaceString(
ExpandConstant('D:\authorized\Builds\Solo\custom.bat'),
'set JRE_HOME=%INSTALL_DIR%\java'#13#10,
'set JRE_HOME='+Java32+#13#10)