Search code examples
inno-setuppascalscript

How to run program or a batch file from Code section of Inno Setup?


How can I insert Run (unzip.exe and a batch file) within the Code section instead of Run? I attempted the approach used here Inno Setup: Install other installer and run it before continuing my install but unable to get it to work so I reverted to using the Run section to run two scripts. What I have done so far appears sloppy. The Inno Setup "finished" page displays an option checkbox to run the batch script, whereas I'd prefer it to run automatically before reaching this stage.

[Setup]
PrivilegesRequired=admin

[Files]
Source: "CC.exe"; DestDir: "{pf}\CC"; DestName: "CC.exe"
Source: "bbb.update.zip"; DestDir: "{userdesktop}"; Flags: deleteafterinstall
Source: "unzip.exe"; DestDir: "{userdesktop}"; Flags: deleteafterinstall

[Run]
Filename: "{userdesktop}\unzip.exe"; \
    Parameters: "x {userdesktop}\bbb.update.zip -d {userdesktop}"; \
    Flags: runascurrentuser nowait
Filename: "{userdesktop}\update.bat"; \
    Flags: runascurrentuser nowait postinstall skipifsilent

(the update.bat file cleans up after installing)


Solution

  • Use Exec function. For example in CurStepChanged event function.

    Also you need to wrap the paths in command parameters to quotes, in case they contain spaces.

    [Code]
    
    procedure CurStepChanged(CurStep: TSetupStep);
    var
      ErrorCode: Integer;
    begin
      if CurStep = ssPostInstall then
      begin
        Exec(
          ExpandConstant('{userdesktop}\unzip.exe'),
          ExpandConstant('x "{userdesktop}\bbb.update.zip" -d "{userdesktop}"'),
          '', SW_HIDE, ewNoWait, ErrorCode);
    
        Exec(
          ExpandConstant('{userdesktop}\update.bat'), '', '', SW_HIDE, ewNoWait, ErrorCode);
      end;
    end;