Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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
share|improve this answer
Edited my answer to add in a few other possibilities. – 8088 Mar 30 '11 at 14:57
Beautiful, thank you. – daxlerod Mar 30 '11 at 15:29
+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. – Craig Ringer Dec 20 '12 at 2:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .