George Gill
George Gill

Reputation: 43

WMIC - bat file menu program

I'm trying to write a batch file that will uninstall programs by selecting a number. I don't know how to count how many programs in the operating system and then to assign numbers to all of them in a MENU format. I don't want to have to type in all of the letters to a program.

Copy and past the below program into notepad and save as GUninstall.bat

@Echo off
Echo This is a batch file uninstallation program. 
Echo Run as administrator WMIC will not work. 
echo.
Echo The command [wmic product get name] will run.
Echo Looking up all installed programs...
echo. 
wmic product get name

echo 1. First program
echo 2. Second program
echo 3. Third program
echo 4. Fourth program
echo 5. Fifth program
echo.
@echo Pick a number: 
echo. 
choice /c:12345 

if "%errorlevel%"=="1" wmic product where name="First program" call uninstall
if "%errorlevel%"=="2" wmic product where name="Second program" call uninstall
if "%errorlevel%"=="3" wmic product where name="Third program" call uninstall
if "%errorlevel%"=="4" wmic product where name="Fourth program" call uninstall
if "%errorlevel%"=="5" wmic product where name="Fifth program" call uninstall

Echo.
Echo.

@echo First method is done. I'll go into the alternate method. 

pause
Echo Get user input - program name?
Echo.
Echo This is an alternate method 
:input
set INPUT=
set /P INPUT=Uninstall which program?: %=%
if "%INPUT%"=="" goto input
echo Your input was: %INPUT%

echo.
echo.
Echo Uninstalling... 

echo The command [wmic product where name="%INPUT%" call uninstall] will run. 


wmic product where name="%INPUT%" call uninstall

@echo If there is "no instance" errors, then the program %INPUT% was uninstalled.

pause

Upvotes: 0

Views: 2742

Answers (1)

jeb
jeb

Reputation: 82327

FOR-Loops are the answers for the most of your problems.

A FOR/F-loop can collect all programs and count them.

set count=0
for /F "delims=" %%A in ('wmic product get name' ) do (
  set /a count+=1
  setlocal EnableDelayedExpansion
  for %%n in (!count!) do (
    endlocal
    set product[%%n]=%%A
    echo %%n. %%A
  )
)

And later you can access them from the array via

SET /p uninstallNr=Uninstall number:
setlocal EnableDelayedExpansion
ECHO !product[%uninstallNr%]!

Upvotes: 1

Related Questions