如何从set /P
提示中选择多个输入?
如果我将此作为我的批处理文件,我的意思是下面的例子:
set /p pcws="Please select 1-4: "
if %pcws%==1 goto 1
if %pcws%==2 goto 2
if %pcws%==3 goto 3
if %pcws%==4 goto 4
:1
echo.
echo Wrong Choice
pause
exit
:2
echo.
echo Thanks
pause
exit
:3
echo.
echo Try again
pause
exit
:4
echo.
echo Have a nice day
pause
exit
如何编写此选项以选择多个输入,例如2
和 4
?
答案 0 :(得分:1)
以下是批量代码的重写和评论,以接受1
,2
,3
或4
的任意组合以及任何无效输入。
@echo off
rem Enable delayed expansion as the user as the freedom to enter any string
rem like an empty string or a string containing 1 or more double quotes or
rem command line operators which could result in undefined behavior or exit
rem of batch execution because of a syntax error on evaluation without using
rem delayed expansion. Please note that not escaped exclamation marks are
rem not interpreted anymore as literal characters, but as begin or end of
rem a delayed expanded environment variable reference.
setlocal EnableExtensions EnableDelayedExpansion
:UserPrompt
cls
set "ValidChoice=0"
set "pcws="
set /P "pcws=Please select 1-4: "
rem Was anything entered by the user at all?
if not defined pcws goto UserPrompt
rem The 4 IF conditions below compare if the user input string with all
rem 1, 2, 3 or 4 replaced by nothing is not equal with the unmodified
rem user input string which means it checks if the user input string
rem contains one or more times 1, 2, 3 or 4.
:UserChoice
echo/
if not "!pcws:1=!" == "!pcws!" set "ValidChoice=1" & goto Choice1
if not "!pcws:2=!" == "!pcws!" set "ValidChoice=2" & goto Choice2
if not "!pcws:3=!" == "!pcws!" set "ValidChoice=3" & goto Choice3
if not "!pcws:4=!" == "!pcws!" set "ValidChoice=4" & goto Choice4
rem Has the user entered at least one of the requested characters?
if %ValidChoice% == 0 goto UserPrompt
endlocal
goto :EOF
:OneMoreChoice
echo/
pause
rem Remove all 1, 2, 3 or 4 from user input string.
set "pcws=!pcws:%ValidChoice%=!"
rem Is the environment variable still defined which means the
rem user input string contains perhaps more choices to process?
if defined pcws goto UserChoice
endlocal
goto :EOF
:Choice1
echo Wrong choice^^!
goto OneMoreChoice
:Choice2
echo Thanks.
goto OneMoreChoice
:Choice3
echo Please try again.
goto OneMoreChoice
:Choice4
echo Have a nice day.
goto OneMoreChoice
在批处理文件执行时,用户可以输入
"<>|
...再次显示提示; 4321
...输出所有4条消息,批处理文件处理结束; 1>nul
...输出第一条消息,批处理文件处理结束; 2,3
...输出第二条和第三条消息,批处理文件处理结束; 2-4
...输出第二条和第四条消息,批处理文件处理结束。也可以在命令行后面的标签UserChoice
上方的注释行上方插入:
rem Accept only a user input containing the digits 1-4 without any other
rem character between or with space(s) or comma(s) anywhere in string.
for /F "delims=,1234 " %%I in ("!pcws!") do goto UserPrompt
如果输入字符串包含除UserPrompt
之外的任何其他字符或空格字符,则附加的 FOR 命令行会跳转到标签,1234
。使用其他 FOR 命令行,将不再接受测试输入1>nul
和2-4
。只有4321
,2,3
和1,3 4, 2
仍然会被解释为进一步处理的有效输入字符串。
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
cls /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
另请参阅Single line with multiple commands using Windows batch file以获取用于在一个命令行上运行两个命令的运算符&
的说明,以避免使用命令块。
另请阅读DosTips论坛主题ECHO. FAILS to give text or blank line - Instead use ECHO/为什么最好使用echo/
代替echo.
输出空行。