2012-07-13 4 views
0

숫자를 선택하여 프로그램을 제거 할 배치 파일을 작성하려고합니다. 운영 체제에서 얼마나 많은 프로그램을 계산 한 다음 모든 프로그램에 MENU 형식으로 번호를 할당하는 방법을 모르겠습니다. 나는 프로그램에 모든 글자를 입력하고 싶지 않습니다.WMIC - bat 파일 메뉴 프로그램

복사하여 아래 프로그램 과거 메모장에와 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 저장은 문제의 가장에 대한 답변입니다.

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%]! 
를 통해 배열에서 액세스 할 수 있습니다