问题
I have a menu here that you can navigate using keys using the choice command. The problem is that I want this menu to have a variable number of options. At the moment, if there are more than 3 drives detected, it will list only 3 of them. If there are less, it will list some options of N/A. Is there an easy way (without set /p) to make this exact menu with the same navigation method, but with variable options? Here's the link to the code I'm talking about
: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
回答1:
Should anyone be looking for a variable menu navigation through choice, it is easily possible to build a variable menu using choice, String concatenation and substring modification.
The below is a simple example script that demonstrates a subroutine that builds and outputs the options available for the current menu and modifies the choice :C definition according the menu options the subroutine is called with.
@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
来源:https://stackoverflow.com/questions/43990813/batch-file-make-variable-menu