如何制作使用数字和数字的.bat菜单同一个多选菜单中的字母?

时间:2017-07-27 03:13:29

标签: batch-file menu

我正在制作一个小型(ish)实用程序批处理文件。 我有一个主要使用多选菜单,使用单键击(无输入)。 我想在第一组选择中使用数字&然后有h& q寻求帮助&分别退出选项。但是当我在测试和运行中运行.bat时选择任何选项,出现两行文字,一行说" h此时出乎意料" &安培;关于q的第二个说法是相同的。

选项似乎工作正常,但这些消息很烦人,为什么它们甚至出现在&我怎么让它们消失?

这是我的代码。

echo off
cls
:menu1
set mypath=%~dp0
@echo %mypath%
echo.
echo .............
echo intro
echo .............
echo.
echo -more menu text here
echo.   
echo 1 - option
echo 2 - option
echo 3 - option
echo 4 - option
echo 5 - option
echo 6 - option
echo 7 - option
echo h - Help
echo q - Quit Program
echo.
choice /c:1234567hq
if errorlevel 1 set m=1
if errorlevel 2 set m=2
if errorlevel 3 set m=3
if errorlevel 4 set m=4
if errorlevel 5 set m=5
if errorlevel 6 set m=6
if errorlevel 7 set m=7
if errorlevel h set m=h
if errorlevel q set m=q
if %m%==1 goto test
if %m%==2 goto test
if %m%==3 goto test
if %m%==4 goto test
if %m%==5 goto test
if %m%==6 goto test
if %m%==7 goto test
if %m%==h goto test
if %m%==q goto test
:test
echo.
echo Aha! A test message!
echo.
pause
goto menu1

我想使用h& amp;问是因为我计划有第二个菜单,其中还有其中的选项。我希望他们也在该菜单中使用相同的键。我试图让这个变得简单& "用户友好"如果可行的话,我也不想为所有选择都有任意字母。

它们不会出现在常规菜单中(带有回车),但是如果可能的话,我想让这个主菜单在单击键上运行命令。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

errorlevel是指选项列表中项目的索引,它始终是一个数字。在您的示例中,h为8,q为9。

此外,if errorlevel [number]表示“如果错误级别为[数字] 或更高”,那么要么需要向后列出选项,要么需要使用%errorlevel%系统变量。最简单的方法是简单地将m设置为%errorlevel%并删除所有goto,因为它们之间没有代码和它们所引用的标签。

@echo off
cls
:menu1
set mypath=%~dp0
@echo %mypath%
echo.
echo .............
echo intro
echo .............
echo.
echo -more menu text here
echo.   
echo 1 - option
echo 2 - option
echo 3 - option
echo 4 - option
echo 5 - option
echo 6 - option
echo 7 - option
echo h - Help
echo q - Quit Program
echo.
choice /c:1234567hq
set m=%errorlevel%

:test
echo.
echo Aha! A test message!
echo.
pause
goto menu1
相关问题