How to declare an Inno Setup preprocessor variable by reading from a file

In the event you want to do this at the installer's run time (@jachguate's answer covers the other possibility, and is probably the one you're looking for - I'll leave this in case it helps others at some point), you can use it's Pascal Script to do so. Something like this should work:

[Setup]
AppVer={code:MyVersion}

[Files]
// Other files
Source: "YourVersionFile.txt"; DestDir: "{app}"

[Code]
function MyVersion(Param: String): String;
var
  VersionValue: string;
  VersionFile: string;
begin
  // Default to some predefined value.
  Result := '0.0.0';
  VersionFile := '{app}\YourVersionFile.txt';
  if LoadStringFromFile(VersionFile, VersionValue) then
    Result := VersionValue;
end;

See the help file topic "Pascal Scripting" for more information, including a list of the supported built-in functions.


From: http://www.jrsoftware.org/ishelp/

A C-like #include directive is supported, which pulls in lines from a separate file into the script at the position of the #include directive. The syntax is:

#include "filename.txt"

If the filename is not fully qualified, the compiler will look for it in the same directory as the file containing the #include directive. The filename may be prefixed by compiler:, in which case it looks for the file in the Compiler directory.


You can use the Inno Setup Pre Proccessor (ISPP) GetFileVersion function to get the version directly from your executable file, like this:

[Setup]
#define AppVersion GetFileVersion("MyFile.exe")
#define AppName "My App name"

AppName={#AppName}
AppVerName={#AppName} {#AppVersion}
VersionInfoVersion={#AppVersion}

; etc

If you're using defines, you're already using ISPP.


Using ISPP's GetFileVersion function is the preferred method (since your installer version should match your application's version, after all). So if this is what you actually wanted to do, you should accept jachguate's answer.

In case you really do want to read the version from a text file instead of from the executable file, then there are two possibilities:

The first: If you can modify the internal format of the file, then you can simplify things considerably by making it look like an INI file:

[Version]
Ver=0.0.11

Given this, you can use ISPP's ReadIni function to retrieve the version:

#define AppVer ReadIni("ver.ini", "Version", "Ver", "unknown")

The second alternative, if you can't change the file format, is to use the FileOpen, FileRead, and FileClose ISPP functions, eg:

#define VerFile FileOpen("ver.txt")
#define AppVer FileRead(VerFile)
#expr FileClose(VerFile)
#undef VerFile

I repeat, though: it's better to get the app version from the executable file itself instead. This helps to ensure that everything matches up, for one thing.

Tags:

Inno Setup