Batch file 如何在批处理文件中创建工作菜单?

Batch file 如何在批处理文件中创建工作菜单?,batch-file,Batch File,我使用的代码是: @echo off SETLOCAL EnableDelayedExpansion for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do ( set "DEL=%%a" ) goto :Start :ColorText echo off <nul set /p ".=%DEL%" > "%~2" findstr

我使用的代码是:

@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)
goto :Start

:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

:Start
:menu1
set /a select = 0
cls
call :ColorText 0a "Menu"
echo.
call :ColorText fb "Option1"
echo.
call :ColorText 0b "Option2"
echo.
call :ColorText 0b "Option3"
echo.
call :ColorText 0e "(W and S to scroll and Q to select)"
choice /c:wsq /n > nul
if errorlevel == w goto menu3
if errorlevel == s goto menu2
if errorlevel == q goto Option1
else goto menu1
:Option1
cls
echo This is Option1.
pause
goto Start
:menu2
set /a select = 0
cls
call :ColorText 0a "Menu"
echo.
call :ColorText 0b "Option1"
echo.
call :ColorText fb "Option2"
echo.
call :ColorText 0b "Option3"
echo.
call :ColorText 0e "(W and S to scroll and Q to select)"
choice /c:wsq /n > nul
if errorlevel w{
set /a select = 1
}
if errorlevel s{
set /a select = 3
}
if errorlevel q{
goto Option2
}
if %select%==1 goto menu1
if %select%==3 goto menu3
:menu3
set /a select = 0
cls
call :ColorText 0a "Menu"
echo.
call :ColorText 0b "Option1"
echo.
call :ColorText 0b "Option2"
echo.
call :ColorText fb "Option3"
echo.
call :ColorText 0e "(W and S to scroll and Q to select)"
choice /c:wsq /n > nul
if errorlevel w{
set /a select = 2
}
if errorlevel s{
set /a select = 1
}
if errorlevel q{
goto Option3
}
if %select%==1 goto menu1
if %select%==2 goto menu2
:Option2
cls
echo This is Option2.
pause
goto Start
:Option3
cls
echo This is Option3.
pause
goto Start
@echo关闭
SETLOCAL EnableDelayedExpansion
对于/F“tokens=1,2 delims=#”%%a in(‘“prompt#$H#$E#&echo on&for%%b in(1)do rem“’)do(
设置“DEL=%%a”
)
后藤:开始
:彩色文本
回音
"%~2"
findstr/v/a:%1/R“^$”“%~2”个
删除“%~2”>nul 2>&1
后藤:eof
:开始
:菜单1
设置/a选择=0
cls
调用:彩色文本0a“菜单”
回声。
呼叫:ColorText fb“选项1”
回声。
呼叫:彩色文本0b“选项2”
回声。
呼叫:彩色文本0b“选项3”
回声。
调用:ColorText 0e“(滚动W和S,选择Q)”
选项/c:wsq/n>nul
如果errorlevel==w转到菜单3
如果errorlevel==s转到菜单2
如果errorlevel==q转到选项1
否则转到菜单1
:选项1
cls
这是选项1。
暂停
开始
:menu2
设置/a选择=0
cls
调用:彩色文本0a“菜单”
回声。
调用:彩色文本0b“选项1”
回声。
呼叫:ColorText fb“选项2”
回声。
呼叫:彩色文本0b“选项3”
回声。
调用:ColorText 0e“(滚动W和S,选择Q)”
选项/c:wsq/n>nul
如果错误级别为w{
设置/a选择=1
}
如果错误级别为{
设置/a选择=3
}
如果错误级别为q{
转到选项2
}
如果%select%==1转到菜单1
如果%select%==3转到菜单3
:菜单3
设置/a选择=0
cls
调用:彩色文本0a“菜单”
回声。
调用:彩色文本0b“选项1”
回声。
呼叫:彩色文本0b“选项2”
回声。
呼叫:ColorText fb“选项3”
回声。
调用:ColorText 0e“(滚动W和S,选择Q)”
选项/c:wsq/n>nul
如果错误级别为w{
设置/a选择=2
}
如果错误级别为{
设置/a选择=1
}
如果错误级别为q{
转到选项3
}
如果%select%==1转到菜单1
如果%select%==2转到菜单2
:选项2
cls
这是选项2。
暂停
开始
:选项3
cls
这是选项3。
暂停
开始
当我使用w、s或q时,我不断地得到一个错误。它一直跳过它应该去的地方,并使用它下面的代码

我该怎么做才能让它成为真正的标签呢


如果可能,我可以在命令中使用enter键和箭头键而不是w、s或q吗?

错误级别始终是一个数值

选项
中定义的第一个
字符
(在您的情况下为w)将
错误级别设置为1

选项中定义的第二个
字符
(在您的情况下为s)将
错误级别设置为2

……等等

因此:


errorlevel
始终是一个数值

选项
中定义的第一个
字符
(在您的情况下为w)将
错误级别设置为1

选项中定义的第二个
字符
(在您的情况下为s)将
错误级别设置为2

……等等

因此:


这是一个有趣的想法
:)
我会这样做:

@echo off
setlocal EnableDelayedExpansion

rem Define the array with options
set "n=0"
for %%a in (Option1 Option2 Option3) do (
   set /A n+=1
   set "option[!n!]=%%a"
)

rem Define selected/unselected colors
set "selected=FB"
set "unselected=0B"

for /F %%a in ('echo prompt $H ^| cmd') do set "DEL=%%a"
goto :Start


:ColorText
<NUL set /P "=%DEL% " > "%~2"
findstr /V /A:%1 /R "^$" "%~2" NUL
del "%~2" > NUL 2>&1
exit /B


:Start
set /A "select=1, newSelect=1"
for /L %%i in (2,1,%n%) do set "color[%%i]=%unselected%"

:menu
set "color[%select%]=%unselected%"
set "select=%newSelect%"
set "color[%select%]=%selected%"
cls
call :ColorText 0A "Menu"
echo/
for /L %%i in (1,1,%n%) do (
   call :ColorText !color[%%i]! "!option[%%i]!"
   echo/
)
call :ColorText 0E "(W and S to scroll and Q to select)"

:choice
choice /C:WSQ /N > NUL
goto Key-%errorlevel%

:Key-1  W = Up menu
if %select% equ 1 goto choice
set /A newSelect=select-1
goto menu

:Key-2  S = Down menu
if %select% equ %n% goto choice
set /A newSelect=select+1
goto menu

:Key-3  Q = Select current option
echo/
echo This is !option[%select%]!
pause
goto Start
@echo关闭
setlocal EnableDelayedExpansion
rem使用选项定义数组
设置“n=0”
对于(选项1选项2选项3)中的%%a,请执行以下操作(
设置/A n+=1
设置“选项[!n!]=%%a”
)
rem定义选定/未选定的颜色
设置“已选=FB”
设置“未选择=0B”
对于('echo prompt$H^ | cmd')中的/F%%a,请设置“DEL=%%a”
后藤:开始
:彩色文本
"%~2"
findstr/V/A:%1/R“^$”“%~2”个
删除“%~2”>NUL 2>&1
退出/B
:开始
设置/A“选择=1,新闻选择=1”
对于(2,1,%n%)中的/L%%i,请设置“颜色[%%i]=%unselected%”
:菜单
设置“颜色[%select%]=%unselected%”
设置“选择=%newSelect%”
设置“颜色[%select%]=%selected%”
cls
调用:彩色文本0A“菜单”
回音/
对于(1,1,%n%)中的/L%%i,请执行以下操作(
调用:ColorText!颜色[%%i]!“!选项[%%i]!”
回音/
)
调用:ColorText 0E“(滚动W和S,选择Q)”
:选择
选项/C:WSQ/N>NUL
转到键-%errorlevel%
:键-1 W=向上菜单
如果%select%eq 1转到选项
set/A newSelect=select-1
转到菜单
:键-2 S=下拉菜单
如果%select%eq%n%goto choice
set/A newSelect=select+1
转到菜单
:Key-3 Q=选择当前选项
回音/
这是回声!选项[%select%]!
暂停
开始

这是一个有趣的想法
:)
我会这样做:

@echo off
setlocal EnableDelayedExpansion

rem Define the array with options
set "n=0"
for %%a in (Option1 Option2 Option3) do (
   set /A n+=1
   set "option[!n!]=%%a"
)

rem Define selected/unselected colors
set "selected=FB"
set "unselected=0B"

for /F %%a in ('echo prompt $H ^| cmd') do set "DEL=%%a"
goto :Start


:ColorText
<NUL set /P "=%DEL% " > "%~2"
findstr /V /A:%1 /R "^$" "%~2" NUL
del "%~2" > NUL 2>&1
exit /B


:Start
set /A "select=1, newSelect=1"
for /L %%i in (2,1,%n%) do set "color[%%i]=%unselected%"

:menu
set "color[%select%]=%unselected%"
set "select=%newSelect%"
set "color[%select%]=%selected%"
cls
call :ColorText 0A "Menu"
echo/
for /L %%i in (1,1,%n%) do (
   call :ColorText !color[%%i]! "!option[%%i]!"
   echo/
)
call :ColorText 0E "(W and S to scroll and Q to select)"

:choice
choice /C:WSQ /N > NUL
goto Key-%errorlevel%

:Key-1  W = Up menu
if %select% equ 1 goto choice
set /A newSelect=select-1
goto menu

:Key-2  S = Down menu
if %select% equ %n% goto choice
set /A newSelect=select+1
goto menu

:Key-3  Q = Select current option
echo/
echo This is !option[%select%]!
pause
goto Start
@echo关闭
setlocal EnableDelayedExpansion
rem使用选项定义数组
设置“n=0”
对于(选项1选项2选项3)中的%%a,请执行以下操作(
设置/A n+=1
设置“选项[!n!]=%%a”
)
rem定义选定/未选定的颜色
设置“已选=FB”
设置“未选择=0B”
对于('echo prompt$H^ | cmd')中的/F%%a,请设置“DEL=%%a”
后藤:开始
:彩色文本
"%~2"
findstr/V/A:%1/R“^$”“%~2”个
删除“%~2”>NUL 2>&1
退出/B
:开始
设置/A“选择=1,新闻选择=1”
对于(2,1,%n%)中的/L%%i,请设置“颜色[%%i]=%unselected%”
:菜单
设置“颜色[%select%]=%unselected%”
设置“选择=%newSelect%”
设置“颜色[%select%]=%selected%”
cls
调用:彩色文本0A“菜单”
回音/
对于(1,1,%n%)中的/L%%i,请执行以下操作(
调用:ColorText!颜色[%%i]!“!选项[%%i]!”
回音/
)
调用:ColorText 0E“(滚动W和S,选择Q)”
:选择
选项/C:WSQ/N>NUL
转到键-%errorlevel%
:键-1 W=向上菜单
如果%select%eq 1转到选项
set/A newSelect=select-1
转到菜单
:键-2 S=下拉菜单
如果%select%eq%n%goto choice
set/A newSelect=select+1
转到菜单
:Key-3 Q=选择当前选项
回音/
这是回声!选项[%select%]!
暂停
开始

打开cmd提示符并键入:
choice/?
阅读帮助文件中的注释。另外,用
()
而不是
{}
将代码括起来,否则转到菜单1的语法错误(键入
如果/?
),只需将
goto menu1
改为…打开一个cmd提示符并键入:
choice/?
阅读帮助文件中的说明。另外,用
)支撑代码,而不是
{}
否则goto menu1
语法错误(键入
如果/?
),只需将
转到menu1
改为。。。