Search code examples
inno-setuppascalscript

Read text file line by line in Inno Setup


I'm trying to read a text file line by line using Inno Setup.

I have tried this one that mentioned here: https://jrsoftware.org/ispphelp/index.php?topic=fileread

function ShowLines(): Boolean;
var
  list: Integer;
begin
  list := FileOpen(ExpandConstant('{tmp}\file.txt'));

  if !FileEof(list) then begin
    try
      repeat
         MsgBox(FileRead(list), mbInformation, MB_OK);
      until !FileEof(list);
    finally
      FileClose(list);
    end;
  end;
  Result := True;
end;

But it will give error, on FileOpen (and maybe on other File functions) that it is an unknown identifier. Where is the problem?

The file is less than 50kb.


Solution

  • All the functions you are trying to call from Pascal Script are actually preprocessor functions. The Pascal Script does not have built-in function that can read file by line (or any kind of chunk).

    You can implement that using WinAPI file functions, like CreateFile and ReadFile.

    But if the file is not too big, you can simply use built-in function LoadStringsFromFile. For an example, see Read strings from file and give option to choose installation.

    Similar question: "Unknown Identifier 'FileOpen'" when trying to detect locked file in Inno Setup code.


    Seeing that you are reading the file from {tmp}, chances are that you are actually reading a temporary file extracted from the installer itself. If that's the case, it means that you have the file available on compile time already. In such case, you can indeed use the preprocessor function to read the file on compile-time.

    But that needs code in completely different language/syntax. Some examples: