Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 233
Angel Montes de Oca Avatar asked Jan 23 '26 05:01

Angel Montes de Oca


1 Answers

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.

like image 122
Martin Prikryl Avatar answered Jan 25 '26 06:01

Martin Prikryl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!