Batch file 如何从SET/P提示符中选择多个输入?

Batch file 如何从SET/P提示符中选择多个输入?,batch-file,input,command,prompt,Batch File,Input,Command,Prompt,如何从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 ag

如何从
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

这里是重写并注释的批处理代码,以接受
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
如果输入字符串包含除
、1234
或空格字符以外的任何其他字符,则额外的FOR命令行将导致跳转到label
UserPrompt
。测试输入
1>nul
2-4
将不再被额外的FOR命令行接受。仅
>4321
2,3
和例如
1,3 4,2
仍将被解释为正在进一步处理的有效输入字符串

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • cls/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • 暂停/?
  • rem/?
  • 设置/?
  • setlocal/?
有关用于在一个命令行上运行两个命令以避免使用命令块的运算符
的说明,请参见


还可以阅读DosTips论坛主题为什么使用
echo/
而不是
echo.
来输出空行。

您可以使用
goto%pcws%
而不是所有那些
if
。您也可以使用
choice/N 1234
而不是
set/p pcws=…
goto%errorlevel%
之后信息技术