Search code examples
batch-filecmdinno-setuppascalscript

How to execute a batch file on uninstall in Inno Setup?


I'm using code from How do you execute command line tools without using batch file in Inno Setup response to execute all my batch files on installation (before, after).

Now I want to execute them just when user click "YES" to uninstaller, but can't find a way to do it. It executes before the confirmation

Here is my code from [Code] section:

function InitializeUninstall(): Boolean;
var
  ResultCode : Integer;    
begin
  Result := True;
  Exec(ExpandConstant('{app}\scripts\unset.bat'), '', '',
       SW_HIDE, ewWaitUntilTerminated, ResultCode); 
end;

Solution

  • Move your code to the CurUninstallStepChanged(usUninstall). That event is triggered after the confirmation of uninstallation.

    procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
    var
      ResultCode : Integer;    
    begin
      if CurUninstallStep = usUninstall then
      begin
        Exec(ExpandConstant('{app}\scripts\unset.bat'), '', '',
             SW_HIDE, ewWaitUntilTerminated, ResultCode); 
      end;
    end;
    

    Though it's easier to use [UninstallRun] section.

    [UninstallRun]
    Filename: "{app}\scripts\unset.bat"; Flags: runhidden
    

    The section is also processed after the confirmation, but before any files are uninstalled. See Uninstallation order.


    Note that in general, you should not use batch files. You better script everything in Pascal code. That way you get much more robust code and better error handling.

    Note that ironically, the question you point to was asked to avoid using batch files in Inno Setup.