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

隐身守侯 提交于 2019-11-27 23:22:20

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!