Search code examples
inno-setuppascalscript

Parse key-value text file in Inno Setup for checking version number


I'm creating a Inno Setup installer/updater for my application. Now I need to find a way to check if a new version is available and if it is available it should be installed automatically over the already installed version.

The special case is that the version number is in a file with other data. The file that Inno Setup need to read looks like:

#Eclipse Product File
#Fri Aug 18 08:20:35 CEST 2017
version=0.21.0
name=appName
id=appId

I already found a way to update the application using a script that only read a text file with the version number in it. Inno setup: check for new updates

But in my case it contains more data that the installer does not need. Can someone help me to build a script that can parse the version number out of the file?

The code that I already have looks like:

function GetInstallDir(const FileName, Section: string): string;
var
  S: string;
  DirLine: Integer;
  LineCount: Integer;
  SectionLine: Integer;    
  Lines: TArrayOfString;
begin
  Result := '';
Log('start');
  if LoadStringsFromFile(FileName, Lines) then
  begin
Log('Loaded file');
    LineCount := GetArrayLength(Lines);
    for SectionLine := 0 to LineCount - 1 do

Log('File line ' + lines[SectionLine]);


    if (pos('version=', Lines[SectionLine]) <> 0) then
                begin
                  Log('version found');
                  S := RemoveQuotes(Trim(Lines[SectionLine]));
                  StringChangeEx(S, '\\', '\', True);
                  Result := S;
                  Exit;
                end;
    end;
end;

But when running the script the check for checking if the version string is on the line does not work.


Solution

  • Your code is almost correct. You are only missing begin and end around the code, that you want to repeat in the for loop. So only the Log line repeats; and the if is executed for out-of-the-range LineCount index.

    It becomes obvious, if you format the code better:

    function GetInstallDir(const FileName, Section: string): string;
    var
      S: string;
      DirLine: Integer;
      LineCount: Integer;
      SectionLine: Integer;    
      Lines: TArrayOfString;
    begin
      Result := '';
      Log('start');
      if LoadStringsFromFile(FileName, Lines) then
      begin
        Log('Loaded file');
        LineCount := GetArrayLength(Lines);
        for SectionLine := 0 to LineCount - 1 do
        begin { <--- Missing }
          Log('File line ' + lines[SectionLine] );
    
          if (pos('version=', Lines[SectionLine]) <> 0) then
          begin
            Log('version found');
            S := RemoveQuotes(Trim(Lines[SectionLine]));
            StringChangeEx(S, '\\', '\', True);
            Result := S;
            Exit;
          end;
        end; { <--- Missing }
      end;
    end;