Find and read specific string from config file with Pascal Script in Inno Setup

前端 未结 1 2004
没有蜡笔的小新
没有蜡笔的小新 2020-12-21 02:57

I have quite long config file and I need to extract specific strings from the file. What I want to extract/read is InstallDir for specific number position e.g. for 20540.

相关标签:
1条回答
  • 2020-12-21 03:04

    You'll need to write your own parser. This might be one possible implementation:

    [Code]
    function GetInstallDir(const FileName, Section: string): string;
    var
      S: string;
      DirLine: Integer;
      LineCount: Integer;
      SectionLine: Integer;    
      Lines: TArrayOfString;
    begin
      Result := '';
      S := '"' + Section + '"'; // AddQuotes is broken somehow...
      if LoadStringsFromFile(FileName, Lines) then
      begin
        LineCount := GetArrayLength(Lines);
        for SectionLine := 0 to LineCount - 1 do
          if Trim(Lines[SectionLine]) = S then
          begin
            if (SectionLine < LineCount) and (Trim(Lines[SectionLine + 1]) = '{') then
              for DirLine := SectionLine to LineCount - 1 do
              begin
                if (Pos('"InstallDir"', Lines[DirLine]) > 0) and
                  (StringChangeEx(Lines[DirLine], '"InstallDir"', '', True) > 0) then
                begin
                  S := RemoveQuotes(Trim(Lines[DirLine]));
                  StringChangeEx(S, '\\', '\', True);
                  Result := S;
                  Exit;
                end;
                if Trim(Lines[DirLine]) = '}' then
                  Exit;
              end;
            Exit;
          end;
      end;
    end;
    
    procedure InitializeWizard;
    begin                         
      MsgBox(GetInstallDir('d:\File.almostjson', '20540'), mbInformation, MB_OK);
    end;
    
    0 讨论(0)
提交回复
热议问题