I'm creating a setup script for a program developed by someone else. Their installer won't install over an existing version, so I need to uninstall any previous installations.

The uninstaller is named unins$num.exe, where the $num is a number that seems to increment each time the program is installed, (e.g. unins000.exe, unins001.exe, unins002.exe).

I'm using a MS-DOS batch script at the moment, as other people in my department would have a chance of maintaining it, but if it is much easier in some other language, I'm open to change. The only requirement is that I can't install an interpreter first, so it must be built in to Windows.

My target platforms are Windows XP and Windows 7. Windows Vista support is nice, but not required.

How can I use a wildcard to start the program, so I don't need to list 1000 possible exe's?


This assumes that the target folder would only contain a single unins$num.exe executable, and that the batch script is in the same folder as the uninstaller:

@echo off
FOR /f "tokens=*" %%G IN ('dir /b unins*.exe') DO %%G

In basic terms it just loops through the output of dir /b unins*.exe and executes each result one-by-one.

If you wanted it so that the batch script could be executed from another location (but still kept in the same folder as the uninstallation executable) then you could add %~dp0 (the path of the batch script) to the script:

@echo off
FOR /f "tokens=*" %%G IN ('dir /b %~dp0\unins*.exe') DO %~dp0\%%G

Finally, if you wanted the batch script in a different folder to the uninstallation executable then you just replace %~dp0 with whatever the full path is, e.g. if the uninstaller is in C:\CoolProgram:

@echo off
FOR /f "tokens=*" %%G IN ('dir /b C:\CoolProgram\unins*.exe') DO C:\CoolProgram\%%G
  • Edited my answer to add in a few other possibilities.
    – Gaff
    Mar 30 '11 at 14:57
  • +1 good solution for a problem that shouldn't exist. I'm increasingly temped by option #2: Install msys, use bash. Or option 3: Install PowerShell, use PowerShell. Dec 20 '12 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.