How do I force an uninstall.exe to delete itself?
Have you ever created your own uninstall.exe file, but could not determine how to make it delete itself? It’s a common problem. Although it might sound easy to solve, the problem is complicated by the fact that you cannot delete a file that is in use. With your uninstall.exe running, how can it delete itself? This article gives you the answer and also provides the code to accomplish the task. The solution for forcing a customized uninstall.exe file to delete itself is to create logic in your uninstall which copies the file to a temporary location then executes it. The exact code to append to your uninstall file is provided below. You can modify the code to meet your specific uninstall needs, but you must include the actions in this code in the sequence listed.
Rem This is a sample script; you must include similar logic in your custom uninstall.
Rem This procedure forces the customuninstall.exe to delete itself.
Check free disk space.
Rem Performing a check to see if customuninstall.exe has already executed.
Rem Note that the constinstall is a parameter that is passed in the Else section of this code.
If CMDLINE Equals "constinstall" then
Rem Retrieve the path of Unwise.exe and the install.log.
Get Registry Key Software\Microsoft\Windows\Explorer\Uninstall place in Variable UNINSTPATH
Rem Execute Unwise.exe.
Rem This performs the standard uninstall and deletes the customuninstall.exe.
Execute %UNINSTPATH%\unwise.exe %UNINSTPATH%\install.log
Rem Force the install to exit.
Rem If the customuninstall.exe has not run itself, then this code is executed.
Rem Get a temp name for the customuninstallexe.
Rem Note that the temporary file has a .tmp extension.
Get Temporary Filename into TEMPFILE
Rem Retrieve the path of the customuninstall.exe.
Get System Information into EXELOC
Rem Parse the .tmp extension from the temporary filename.
Parse String "%TEMPFILE%" into TEMPFILENAME and EXT
Rem Append the .exe extension to the temp filename so that the custom uninstall program can be executed.
Set Variable CUSTOMUNINST to %TEMPFILENAME%.EXE (Append)
Rem Copy the customuninstall.exe to a temp location using the temp filename.
Copy local file from %EXELOC% to %TEMP%\%CUSTOMUNINST%
Rem Execute the customuninstall.exe.
Rem NOTE: Remember to pass the customuninstall.exe a command line parameter.
Rem This parameter is used as a flag to indicate that the customuninstall.exe has executed.
Execute %TEMP%\%CUSTOMUNINST% constinstall
Rem Force the installation to exit.
Rem This is the end of the IF statement.