2012-07-13 109 views
0

我想寫一個批處理文件,通過選擇一個數字來卸載程序。我不知道如何計算操作系統中的多少程序,然後以MENU格式將數字分配給所有程序。我不想輸入程序的所有字母。WMIC - 蝙蝠文件菜單程序

複製和過去下面的程序到記事本並保存爲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 

回答

1

FOR -Loops大多爲你的問題的答案。

A FOR/F -loop可以收集所有程序並對它們進行計數。

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 
) 
) 

,以後你可以從陣列通過

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