批处理文件上有多个选项菜单?

时间:2013-01-25 19:44:29

标签: batch-file

您好我想制作一个批处理文件菜单,询问“选择您要安装的应用程序吗?”例如

  1. App1的
  2. App2的
  3. App3的
  4. APP4
  5. APP5
  6. 所有应用
  7. 选择适合的应用:_

    我想要的是,例如我想安装App2,App3和App5,因此我可以通过App ID输入'选择适合的应用程序:2,3,5'。当用户选择选项6时,它将安装所有应用程序!

    我知道这可以在bash脚本中使用,但我不确定批处理脚本吗?

    批处理菜单的示例是http://mintywhite.com/software-reviews/productivity-software/create-multiple-choice-menu-batchfile/

13 个答案:

答案 0 :(得分:7)

答案

这将做你想要的。如果您有任何疑问,请告诉我。您所要做的就是按照脚本中列出的两个步骤进行操作。

脚本

:: Hide Command and Set Scope
@echo off
setlocal EnableExtensions

:: Customize Window
title My Menu

:: Menu Options
:: Specify as many as you want, but they must be sequential from 1 with no gaps
:: Step 1. List the Application Names
set "App[1]=One"
set "App[2]=Two"
set "App[3]=Three"
set "App[4]=Four"
set "App[5]=Five"
set "App[6]=All Apps"

:: Display the Menu
set "Message="
:Menu
cls
echo.%Message%
echo.
echo.  Menu Title
echo.
set "x=0"
:MenuLoop
set /a "x+=1"
if defined App[%x%] (
    call echo   %x%. %%App[%x%]%%
    goto MenuLoop
)
echo.

:: Prompt User for Choice
:Prompt
set "Input="
set /p "Input=Select what app:"

:: Validate Input [Remove Special Characters]
if not defined Input goto Prompt
set "Input=%Input:"=%"
set "Input=%Input:^=%"
set "Input=%Input:<=%"
set "Input=%Input:>=%"
set "Input=%Input:&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
call :Validate %Input%

:: Process Input
call :Process %Input%
goto End


:Validate
set "Next=%2"
if not defined App[%1] (
    set "Message=Invalid Input: %1"
    goto Menu
)
if defined Next shift & goto Validate
goto :eof


:Process
set "Next=%2"
call set "App=%%App[%1]%%"

:: Run Installations
:: Specify all of the installations for each app.
:: Step 2. Match on the application names and perform the installation for each
if "%App%" EQU "One" echo Run Install for App One here
if "%App%" EQU "Two" echo Run Install for App Two here
if "%App%" EQU "Three" echo Run Install for App Three here
if "%App%" EQU "Four" echo Run Install for App Four here
if "%App%" EQU "Five" echo Run Install for App Five here
if "%App%" EQU "All Apps" (
    echo Run Install for All Apps here
)

:: Prevent the command from being processed twice if listed twice.
set "App[%1]="
if defined Next shift & goto Process
goto :eof


:End
endlocal
pause >nul

答案 1 :(得分:5)

您可以使用choice.exe,请参阅此处:http://ss64.com/nt/choice.html

答案 2 :(得分:3)

您想使用set /p示例:

echo What would you like to install?
echo 1 - App1
echo 2 - App2

set /p whatapp=

if %whatapp%==1 (
    codetoinstallapp1
) else if %whatapp%==2 (
    codetoinstallapp2
) else (
    echo invalid choice
)

答案 3 :(得分:2)

@echo off
:menu
cls
echo.
echo Select the case color you want to create:
echo ==========================================
echo.
echo App 1
echo App 2
echo App 3
echo App 4
echo.
echo ==========================================
echo Please answer Y/N to the following:

set /p App1= Install App 1?
set /p App2= Install App 2?
set /p App3= Install App 3?
set /p App4= Install App 4?

if /I "%App1%" EQU "Y" goto :Option-1
if /I "%App1%" EQU "N" goto :1
:1
if /I "%App2%" EQU "Y" goto :Option-2
if /I "%App2%" EQU "N" goto :2
:2
if /I "%App3%" EQU "Y" goto :Option-3
if /I "%App3%" EQU "N" goto :3
:3
if /I "%App4%" EQU "Y" goto :Option-4
if /I "%App4%" EQU "N" goto :End



:Option-1
App 1 Loc.
goto 1
:Option-2
App 2 Loc.
goto 2
:Option-3
App 3 Loc.
goto 2
:Option-4
App 4 Loc.
:End

Exit

答案 4 :(得分:1)

这是我学到的一个技巧:

echo.1) first choice
echo.2) second choice
echo.3) third choice
echo.4) fourth choice

:: the choice command

set pass=
choice /c 1234 /n /m "Choose a task"
set pass=%errorlevel%

::the choices

if errorlevel 1 set goto=1
if errorlevel 2 set goto=2
if errorlevel 3 set goto=3
if errorlevel 4 set goto=4
goto %goto%

虽然我只使用1-4,但添加更多可能的选择会很容易。

答案 5 :(得分:0)

批处理文件是命令提示符命令的列表。以下代码打印到终端:

echo whateveryouwant

使用批处理文件中的这些echo语句打印菜单。

可以在此处找到用户输入:How to read input from console in a batch file?

应用程序的安装稍微复杂一些 - 您需要知道应用程序的要求以及应该移动文件的位置 - 这也应该很简单;在相应位置的相应文件上使用move

答案 6 :(得分:0)

以下是我正在使用的批处理脚本菜单的示例:

@echo off
setlocal
:begin
cls
echo [LOCAL ACCOUNTS REMOTE ADMIN] --------------------------------------
echo   1 -- List local accounts on a remote machine
echo   2 -- Create a local account on a remote machine
echo   3 -- Change a local account password on a remote machine
echo   4 -- Delete a local account on a remote machine
echo;
echo   5 -- exit
echo;
set /P rmFunc="Enter a choice: "
echo --------------------------------------------------------------------
for %%I in (1 2 3 4 5 x) do if #%rmFunc%==#%%I goto run%%I
goto begin

:run1
rem list local accounts code
goto begin

:run2
rem create local account code
goto begin

rem and so on, until...

:run5
:run9
:run99
:runx
endlocal
goto :EOF

最相关的位是set /p行和for...in行。 for...in行基本上将输入的选项与每个菜单项编号进行比较,如果匹配,则goto run#;否则从头开始。

答案 7 :(得分:0)

我看到上述答案都没有完全回答他/她的问题。他们遗漏的一个功能是一次性选择它安装的所有软件(可以这么说)。

所以我把它放在了头顶上(非常抱歉,如果有问题,我会编辑它,如果有的话)。

@echo off & setlocal enabledelayedexpansion

echo What would you like to install?
::Put your options here, preferably numbered.
set /p op=Type the numbers of the software you want to install (separated by commas with no spaces. E.g: 1,3,2): 

for /f "delims=, tokens=1-5" %%i in ("op") do (
set i=%%i
set j=%%j
set k=%%k
set l=%%l
set m=%%m
)
if %i%X neq X set last=1b & goto %i%
:1b
if %j%X neq X set last=2b & goto %j%
:2b
if %k%X neq X set last=3b & goto %k%
:3b
if %l%X neq X set last=4b & goto %l%
:4b
if %m%X neq X set last=%m% & goto %m%
goto next

:1
::Put the code for doing the first option here
goto %last%
:2
::Put the code for doing the second option here
goto %last%
:3
::Put the code for doing the third option here
goto %last%
:4
::Put the code for doing the fourth option here
goto %last%
:5
::Put the code for doing the fifth option here
goto %last%

:下一个 ::在这里放一些东西......

所以这有点过分了。随意改变一些事情等等。

代码正在做的是获取用户输入(例如,如果你输入“1,3,4”),将每个数字放入自己的变量中,检查该变量是否为空,如果不是,发送给你做任何选项的代码。在完成所有变量评估之前,它会这样做几次。

答案 8 :(得分:0)

这是对David Ruhmann关于上述“验证输入”部分的代码进行改进的建议分析:

测试特殊字符的菜单会产生一种魅力,除了以下字符“^&amp;&lt;”。当每个提交输入时,程序关闭。

set "Input=%Input:"=%"
set "Input=%Input:^^=%"
set "Input=%Input:>=%"
set "Input=%Input:<=%"
set "Input=%Input:^&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"

逃离^和&amp;工作,但解析“&lt;”非常奇怪和“&gt;” (逃避这些似乎不起作用)。如果我们按照上述修正案颠倒两个陈述的顺序,我们会发现“&lt;”有效,但现在“&gt;”没有。

但是,将第二个语句改为“&lt;”因此,两个重定向字符都有效,但现在“)”没有!!

set "Input=%Input:"=%"
set "Input=%Input:^^=%"
set "Input=%Input:>=%"
set "Input=%Input:^&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
set "Input=%Input:<=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"

批处理菜单的另一个很棒的教程是here

答案 9 :(得分:0)

我正在使用这个

@echo off
:a
echo Welcome to a casual log-in (you are a idiot)

echo.

pause


echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

set /p c=Email:

echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

set /p u=Password:

echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

msg * Welcome %c%.

goto a

答案 10 :(得分:0)

带复选框模拟的菜单。

@echo off

set size=3

::preset
set chbox2=x


:prepare
for /L %%i in (0,1,%size%) do (
    if defined chbox%%i (
        set st%%i=Y
    ) else (
        set chbox%%i= 
    )
)


:menu
cls
echo.
echo  1. [%chbox1%]  name_1:
echo.
echo  2. [%chbox2%]  name_2:
echo.
echo  3. [%chbox3%]  name_3:
echo.
echo.
echo.


choice /C 1234567890qa /N /M "Select [1-9] >> [a]pply or [q]uit:"
echo.
set inp=%errorlevel%

if %inp%==11 (
    exit
)
if %inp%==12 (
    call :apply
)

::switch
if defined st%inp% (
    set st%inp%=
    set chbox%inp%= 
) else (
    set st%inp%=Y
    set chbox%inp%=X
)
goto :menu


:apply
for /L %%i in (0,1,%size%) do (
    if defined st%%i (
        call :st%%i
        echo.
    )
)
echo.
pause
goto :menu




:st1
echo First Command
goto :eof


:st2
echo Second Command
goto :eof


:st3
echo Third Command
goto :eof

您可以将检查为默认值的行设置为:预设标签。

答案 11 :(得分:0)

实际上有一种非常简单的方法。

@echo off
echo Which app do you want to install?
echo [APP 1]
echo [APP 2]
echo [APP 3]
echo.
echo Type 1, 2, or 3.
set /p "AppInstaller=>"
if %AppInstaller%==1 goto 1
if %AppInstaller%==2 goto 2
if %AppInstaller%==3 goto 3
:1
[INSTALL CODE]
:2
[INSTALL CODE]
:3
[INSTALL CODE]

菜单,如果这样编码,将如下所示:

Which app do you want to install?
[APP 1]
[APP 2]
[APP 3]

Type 1, 2, or 3.
>_

代码将变量AppInstaller设置为1,2或3.文件确定这一点并将您重定向到每个变量的安装程序。

答案 12 :(得分:0)

这是我在多选游戏中大量使用的相当简单的代码:

@echo off
color 0a
cls
:download
echo App 1
echo App 2
echo App 3
echo App 4
echo App 5
echo All Apps
echo 
echo Select What App (1, 2, 3, ect.):
set /p apps=

if %apps%==1 goto 1
if %apps%==1 goto 2
if %apps%==1 goto 3
if %apps%==1 goto 4
if %apps%==1 goto 5
if %apps%==1 goto all

:1
(Your Code Here)
:2
(Your Code Here)
:3
(Your Code Here)
:4
(Your Code Here)
:5
(Your Code Here)
:all
(Your Code Here)