2017-09-04 44 views
0

如何从set /P提示中选择多个输入?如何从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 

我怎么会写这个选择多个输入,例如24

+2

您可以使用'goto%pcws%'而不是所有'if'。你也可以用'choice/N 1234'代替'set/p pcws = ...'和'goto%errorlevel%'。 – Aacini

+0

[可能的重复](https://stackoverflow.com/questions/24972506/batch-scripting-multiple-selection) – Stephan

回答

1

这是重写和批注的批处理代码,以接受1,2,34的任意组合以及任何无效输入。

@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 

附加FOR命令行导致跳转到label UserPrompt如果输入的字符串中包含任何除,1234之外的其他字符或空格字符。将不会再接受测试输入1>nul2-4,并使用附加的FOR命令行。只有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.来输出空行。

相关问题