1

I am trying to read line by line the content of a text file;

I am looking for something, for a specific line.

I need to modify that line;

I need to save, the new content in another file, and then delete the original file and rename the new file with the original file name;

function NextButtonClick(CurPageID: Integer): Boolean;
var
  ExecInfo: TShellExecuteInfo;
  ExecInfoBrowser: TShellExecuteInfo;
  textFileFrom, textFileTo : text;
  line: string;
begin
  Result := True;

  if CurPageID = wpFinished then
  begin
    ExecInfo.cbSize := SizeOf(ExecInfo);
    ExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
    ExecInfo.Wnd := 0;
    ExecInfo.lpFile := ExpandConstant('{app}') + '\{#Exewampmanager}';
    ExecInfo.nShow := SW_HIDE;
    if ShellExecuteEx(ExecInfo) then
      begin
        if WaitForSingleObject(ExecInfo.hProcess, 5000) = WAIT_TIMEOUT then
          begin
            Assign(textFileFrom,'wampmanager.conf');
            Reset(textFileFrom);
            Assign(textFileto,'wampmanager2.conf');
            Rewrite(textFileTo);
            repeat
              readln(textFileFrom,line);
              writeln(textFileto,line);
            until eof(textFileFrom);
            Close(textFileFrom);
            Close(textFileTo);
               ExecInfoBrowser.cbSize := SizeOf(ExecInfo);
               ExecInfoBrowser.fMask := SEE_MASK_NOCLOSEPROCESS;
               ExecInfoBrowser.Wnd := 0;
               ExecInfoBrowser.lpFile := 'http://localhost/cow';
               ExecInfoBrowser.nShow := SW_HIDE;
               ShellExecuteEx(ExecInfoBrowser);
          end;
      end;
  end;
end;

in the file i need to nodify this line: installDir = "c:/wamp"

because the new installation might not be in the same location

this is the conf file:

[main]
language = english
status = "offline"
wampserverVersion = 2.2
wampserverLastKnown = 2.2
installDir = "c:/wamp"
navigator = "C:\Windows\explorer.exe"
defaultLanguage = english


[php]
phpVersion = "5.4.3"
phpLastKnown = 5.4.3
phpIniDir = .
phpConfFile = php.ini
phpExeDir = .


[phpCli]
phpCliVersion = 5.4.3
phpExeFile = php.exe
phpCliFile = php-win.exe


[apache]
apacheVersion = "2.2.22"
apacheLastKnown = 2.2.22
apacheExeDir = bin
apacheConfDir = conf
apacheExeFile = httpd.exe
apacheConfFile = httpd.conf
apacheServiceInstallParams = -n wampapache -k install
apacheServiceRemoveParams = -n wampapache -k uninstall


[mysql]
mysqlVersion = "5.5.24"
mysqlLastKnown = 5.5.24
mysqlConfDir = .
mysqlConfFile = my.ini
mysqlExeDir = bin
mysqlExeFile = mysqld.exe
mysqlServiceInstallParams = --install-manual wampmysqld
mysqlServiceRemoveParams = --remove wampmysqld


[apps]
phpmyadminVersion = 3.5.1
sqlbuddyVersion = 1.3.3
webgrindVersion = 1.0
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100
  • For instance [`this way`](http://pastebin.com/scXy2kYC) (untested, written in web browser). But I'm not posting answer since I assume you need to search that line by something specific. – TLama Oct 10 '13 at 14:23
  • Anyway, are you sure you want to read that config file line by line ? It seems to be the usual INI file format, so maybe we can suggest a better way to work with it (key, value access). – TLama Oct 10 '13 at 18:32
  • 2
    Please don't tag inno-setup questions with "windows installer". The two technologies are unrelated. – Christopher Painter Oct 10 '13 at 19:00

1 Answers1

3

If that config file is in an INI file format, what seems to be, you can either use the [INI] section to modify a single value e.g. this way (note, that you must double double quotes to compile the script and have the value enclosed in double quotes). Of course the values I've showed here you can replace with constants as usually:

[INI]
Filename: "{app}\wampmanager.conf"; Section: "main"; Key: "installDir"; String: """{app}"""

Or you can complicate things a bit by using code from the [Code] section:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
function ChangeInstallDir(const FileName, InstallDir: string): Boolean;
begin
  Result := SetIniString('main', 'installDir', '"' + InstallDir + '"', FileName);
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then
    if not ChangeInstallDir(ExpandConstant('{app}\wampmanager.conf'), 
        ExpandConstant('{app}')) then
      MsgBox('Saving to config file failed!', mbError, MB_OK);
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
TLama
  • 75,147
  • 17
  • 214
  • 392
  • is it important where in the inno file i put the [INI]region and code you gave me? – Ionut Flavius Pogacian Oct 11 '13 at 07:47
  • No, the script sections can be placed wherever you want. But don't be confused. These are two ways to do the same, either `[INI]` section entry, or that code ;-) – TLama Oct 11 '13 at 07:50
  • If I would like to adjust an INI-file based on input in a wizard screen, I think I should use the "Code" option, right? Or can that also be done with the [INI] option? – westr Aug 28 '20 at 06:35