批量文件 - 制作变量菜单

问题描述 投票:0回答:1

我这里有一个菜单,你可以使用选择命令用键来导航。问题是我想让这个菜单有一个可变数量的选项,目前,如果检测到有3个以上的驱动器,它将只列出其中的3个。目前,如果检测到有超过3个驱动器,它将只列出其中的3个。如果数量较少,它将列出一些NA的选项。有没有一个简单的方法(不需要设置p)来制作这个具有相同导航方法的菜单,但是有可变的选项?下面是我所说的代码的链接。

:list1
cls
setlocal EnableDelayedExpansion
echo Drives:
echo -------------------
echo ^> Drive !1a!
echo.
echo   Drive !2a!
echo.
echo   Drive !3a!
echo.
echo Other options:
echo -------------------
echo   Exit
echo.
echo   Back
echo.
echo Use WSX to navigate this menu.
endlocal
choice /c WSX /n >nul
if "%errorlevel%" == "1" goto list5
if "%errorlevel%" == "2" goto list2
if "%errorlevel%" == "3" goto list1a
goto list

:list2
cls
setlocal EnableDelayedExpansion
echo Drives:
echo -------------------
echo   Drive !1a!
echo.
echo ^> Drive !2a!
echo.
echo   Drive !3a!
echo.
echo Other options:
echo -------------------
echo   Exit
echo.
echo   Back
echo.
echo Use WSX to navigate this menu.
endlocal
choice /c WSX /n >nul
if "%errorlevel%" == "1" goto list1
if "%errorlevel%" == "2" goto list3
if "%errorlevel%" == "3" goto list2a
goto list2

:list3
cls
setlocal EnableDelayedExpansion
echo Drives:
echo -------------------
echo   Drive !1a!
echo.
echo   Drive !2a!
echo.
echo ^> Drive !3a!
echo.
echo Other options:
echo -------------------
echo   Exit
echo.
echo   Back
echo.
echo Use WSX to navigate this menu.
endlocal
choice /c WSX /n >nul
if "%errorlevel%" == "1" goto list2
if "%errorlevel%" == "2" goto list4
if "%errorlevel%" == "3" goto list3a
endlocal
goto list3

:list4
cls
setlocal EnableDelayedExpansion
echo Drives:
echo -------------------
echo   Drive !1a!
echo.
echo   Drive !2a!
echo.
echo   Drive !3a!
echo.
echo Other options:
echo -------------------
echo ^> Exit
echo.
echo   Back
echo.
echo Use WSX to navigate this menu.
endlocal
choice /c WSX /n >nul
if "%errorlevel%" == "1" goto list3
if "%errorlevel%" == "2" goto list5
if "%errorlevel%" == "3" goto end
goto list4

:list5
cls
setlocal EnableDelayedExpansion
echo Drives:
echo -------------------
echo   Drive !1a!
echo.
echo   Drive !2a!
echo.
echo   Drive !3a!
echo.
echo Other options:
echo -------------------
echo   Exit
echo.
echo ^> Back
echo.
echo Use WSX to navigate this menu.
endlocal
choice /c WSX /n >nul
if "%errorlevel%" == "1" goto list4
if "%errorlevel%" == "2" goto list1
if "%errorlevel%" == "3" goto menu1
goto list4
windows batch-file
1个回答
0
投票

如果有人正在寻找一个通过选择的可变菜单导航,很容易就可以使用选择、字符串连接和子串修改来建立一个可变菜单。

下面是一个简单的示例脚本,演示了一个子程序,它可以建立和输出当前菜单的可用选项,并根据子程序调用的菜单选项修改choice :C定义。

@Echo off
    Set Echo.ID=Call :ID.Label "%%~1" "%%~2"
    Setlocal EnableDelayedExpansion

    Title Menu Navigation Example
:Bar
    Set "Left.Date="
    IF /I "!Left.Bar!" == "Yes" Goto :Date
    Call :[+Menu] "Bar" "Drink Fight Chat Leave"
Goto :Bar
:Date
    Set "Left.Bar="
    IF /I "!Left.Date!" == "Yes" Goto :Bar
    Call :[+Menu] "Date" "Dinner Flirt Leave"
Goto :Date

:[+Menu] <Location / Core Menu option> <Sub Actions / Menu Items as list>
::: - Build menu and choice list for display
    CLS
    Set "CHOICES="
    Set "Actions="
    For %%A in (%~2) Do (
        Set "Option=%%~A"
        Set Actions=!Actions! "%%~A"
        Set "CHOICES=!CHOICES!!Option:~0,1!"
        Set "Option=[!Option:~0,1!]!Option:~1,100!"
        Echo !Option!
    )
::: - Return key literal from choice command and call relevent menu item with Identifying Params
    Set "Option="
    For /F "delims=" %%C in ('Choice /N /C:%CHOICES%') Do (
        For %%O in (%~2) Do (
            Set "Option=%%~O"
            Set "Option=!Option:~0,1!"
            If /I "!Option!" == "%%C" Call :[%~1_%%~O-%%C] "%%~1 %%~O" "%~1"
        )
    )
Exit /B 0

:[Bar_Drink-D]
    %Echo.ID%
Goto :!Loc!
:[Bar_Fight-F]
    %Echo.ID%
Goto :!Loc!
:[Bar_Chat-C]
    %Echo.ID%
Goto :!Loc!
:[Bar_Leave-L]
    %Echo.ID%
    Set "Left.Bar=Yes"
Goto :!Loc!

:[Date_Dinner-D]
    %Echo.ID%
Goto :!Loc!
:[Date_Flirt-F]
    %Echo.ID%
Goto :!Loc!
:[Date_Leave-L]
    Set "Left.Date=Yes"
    %Echo.ID%
Goto :!Loc!

:ID.Label <%~1> <%~2>
    Title %~1
    Set "Loc=%~2"
Exit /B
© www.soinside.com 2019 - 2024. All rights reserved.