3

In addition to usual install tasks I need to get the value of the command line parameter (for example /MyParam=XXX) and then copy the parameter value (XXX) to a txt file in the app folder. So far I tried the code below but I don't know how to properly pass the parameter value to the code procedure.

[Files]
Source: "MyApp.exe"; DestDir: "{app}"; Flags: ignoreversion overwritereadonly; \
    AfterInstall: SaveParam('{param:MyParam}');

[Code]
procedure SaveParam(S: String);
begin
  if S<>'' then
  begin
    SaveStringToFile('{app}\file.txt', 'Value=' + S, False);
  end;
end;

In short, when I run the installer by MyApp.exe /MyParam=XXX I expect the Value=XXX text would be added into the file.txt file. Any help is highly appreciated, thanks!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Waits321
  • 33
  • 4

1 Answers1

3

Start here: Is it possible to accept custom command line parameters with Inno Setup

Other things you need to know:

  • You have to ExpandConstant the {app} in the file path.
  • You should use CurStepChanged to trigger the writing. Using AfterInstall of an unrelated file works too, but it's more like a hack.
procedure CurStepChanged(CurStep: TSetupStep);
var
  Value: string;
begin
  if CurStep = ssPostInstall then
  begin
    Value := ExpandConstant('{param:MyParam}');
    if (Value <> '') or CmdLineParamExists('MyParam') then
    begin
      Log('Writing "' + Value + '" to file...');
      SaveStringToFile(
        ExpandConstant('{app}\file.txt'), 'Value=' + Value, False);
    end;
  end;
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992