Search code examples
inno-setuppascalscript

How to set my own variable in [Setup] and read it from [Code]


I'm trying to create a script in Inno Setup to pull files from a GitHub repository, extract it, and write it to a directory defined in the [Setup] section.

[Setup]
MyVariable={userappdata}\MetaQuotes\Terminal\{#MyAppName}
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
begin
  // Copy the selected files to the selected directory
  if not FileCopy(Output + '\file1.txt', MyVariable + '\file1.txt', False) then
  begin
    MsgBox('Failed to copy file1.txt to the selected directory.', mbError, MB_OK);
    Abort();
  end;
  if not FileCopy(Output + '\file2.txt', MyVariable + '\file2.txt', False) then
  begin
    MsgBox('Failed to copy file2.txt to the selected directory.', mbError, MB_OK);
    Abort();
  end;
end;

Obviously, this won't compile because MyVariable hasn't been defined in the Pascal script. Only, I'm not sure how to reference the value in [Setup]. Or am I going about this in the wrong way?


Solution

  • Well, you can read the [Setup] section directives with SetupSetting preprocessor function:
    How to read a [Setup] parameter in the Pascal code?

    But you cannot invent your own new directives.


    But you can either:

    • Use preprocessor variable using #define directive (the way you already use it for MyAppName):

      #define MyVariable "{userappdata}\MetaQuotes\Terminal\" + MyAppName
      

      It does not matter where you place the #define as long as it it before you use the variable.

      Use it like this in the Pascal Script:

      ExpandConstant('{#MyVariable}\file1.txt')
      

      The ExpandConstant is to expand the {userappdata}.

    • Use Pascal Script constant:

      [Code]
      
      const 
        MyVariable = '{userappdata}\MetaQuotes\Terminal\{#MyAppName}';
      

      Use it like any other Pascal Script variable/constant.

      ExpandConstant(MyVariable) + '\file1.txt'