2017-09-04 220 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.來輸出空行。

相關問題