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

问题描述 投票:7回答:13

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

  1. APP1
  2. 应用2
  3. APP3
  4. APP4
  5. APP5
  6. 所有应用

选择什么应用程序:_

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

我知道这可以用于bash脚本,但我不确定批处理脚本?

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

batch-file
13个回答
8
投票

回答

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

脚本

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

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

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.文件确定此值并将您重定向到每个变量的安装程序。


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)

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

您可以将下面选中的行设置为默认值:预设标签。


5
投票

你可以使用choice.exe看到这里:http://ss64.com/nt/choice.html


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
)

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

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,但添加更多可能的选择将非常容易。


0
投票

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

echo whateveryouwant

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

获取用户输入可以在这里找到:How to read input from console in a batch file?

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


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#;否则从头开始。


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%

:next ::在这里放一些东西......

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

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


0
投票

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

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

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:^==%"

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

但是,用“<”向下移动第二个语句,两个重定向字符都有效,但现在“)”没有!

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

© www.soinside.com 2019 - 2024. All rights reserved.