首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WMIC - bat文件菜单程序

WMIC - bat文件菜单程序
EN

Stack Overflow用户
提问于 2012-07-13 17:56:10
回答 1查看 2.7K关注 0票数 0

我正在尝试编写一个批处理文件,通过选择一个数字来卸载程序。我不知道如何计算操作系统中有多少程序,然后以菜单格式分配所有程序的编号。我不想把所有的字母都输入到程序中。

复制并通过以下程序到记事本并保存为GUninstall.bat

代码语言:javascript
复制
@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
EN

回答 1

Stack Overflow用户

发布于 2012-07-13 22:57:45

FOR-Loops是大多数问题的答案。

FOR/F-loop可以收集所有程序并对它们进行计数。

代码语言:javascript
复制
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
  )
)

稍后,您可以从数组中通过

代码语言:javascript
复制
SET /p uninstallNr=Uninstall number:
setlocal EnableDelayedExpansion
ECHO !product[%uninstallNr%]!
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11475782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档