如何批量验证输入

时间:2020-02-21 09:29:38

标签: batch-file

所以我批量制作了一个菜单,您可以选择要执行的操作,但是如果u在菜单上未输入的选项将选择第一个选项。如何使它回显无效输入,请重试。
菜单:

echo Please select what you would like to do!
echo =========================================
echo 1)Example 1 
echo 2)Example 2
echo 3)Example 3
echo 4)Example 4
echo 5)Example 5
echo 6)Example 6
echo =========================================
set /p ans="Please enter your selection:"

if %ans%==1 (
goto a
)
if %ans%==2 (
goto b
)
if %ans%==3 (
goto c
)
if %ans%==4 (
goto f
)
if %ans%==5 (
goto g
)
if %ans%==6 (
goto h
)

1 个答案:

答案 0 :(得分:1)

使用选择。简单得多。这是一个例子

@echo off
Choice /c 123456 /m "select choice"
If not %errorlevel% equ 0 Echo you chose %errorlevel%

然后您可以通过创建易于管理的标签将其用于goto

@echo off
Choice /c 123 /m "select choice"
Goto opt%errorlevel%
:opt3
Echo do something here for option 3
Goto :eof
:opt2
Echo do something here for option 2
Goto :eof
:opt1
Echo do something for oprion 1
:opt0
Exho You pressed ctrl+c and selected N
....

You get the idea..