Multiple choices menu on batch file?

后端 未结 13 1600
盖世英雄少女心
盖世英雄少女心 2020-11-28 12:56

Hi I want to make a batch file menu, that asks \'Select app you want to install?\' for example

  1. App1
  2. App2
  3. App3
  4. App4
  5. App5
相关标签:
13条回答
  • 2020-11-28 13:29
    @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
    
    0 讨论(0)
  • 2020-11-28 13:30

    Here's a trick I learned:

    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%
    

    While I use only 1-4 it would be very easy to add more possible choices.

    0 讨论(0)
  • 2020-11-28 13:30

    Menu with analog of checkbox.

    @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
    

    You can set lines checked as defaults under :preset label.

    0 讨论(0)
  • 2020-11-28 13:32

    Here's an example of a batch script menu I'm using:

    @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
    

    The most relevant bits are the set /p line and the for...in lines. The for...in line basically compares the choice entered with every menu item number, and if match, goto run#; otherwise start over from the beginning.

    0 讨论(0)
  • 2020-11-28 13:34

    Answer

    This will do what you want. Let me know if you have any questions. All you have to do is follow the two steps listed in the script.

    Script

    :: 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 讨论(0)
  • 2020-11-28 13:35

    I'm using this

    @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 讨论(0)
提交回复
热议问题