仅使用预定义符号限制 Windows 批处理脚本中的用户输入



我已经为WinPE编写了以下Windows批处理脚本,以使某些任务更容易:

@ECHO OFF
CLS
:MENU
SETLOCAL
SET QUIT=FALSE
SET /P INPUTCHOICES=Type 1, 2, 3 or 4 and press ENTER: 
CALL :EXECUTECHOICES %INPUTCHOICES%
ENDLOCAL
GOTO :EOF
:EXECUTECHOICES
IF [%1]==[] (GOTO :MENU) ELSE (CALL :%1)
SHIFT
GOTO :EXECUTECHOICES
GOTO :EOF
:1
cls
SET /P RB_OR_SD=Type R to reboot or type S to shutdown your PC when done, and press ENTER: 
ECHO Performing task 1
ECHO.
pause
IF "%RB_OR_SD%"=="R" (GOTO :3)
IF "%RB_OR_SD%"=="S" (GOTO :4)
ELSE (GOTO :3)
:2
cls
SET /P RB_OR_SD=Type R to reboot or type S to shutdown your PC when done, and press ENTER: 
ECHO Performing task 2
ECHO.
pause
IF "%RB_OR_SD%"=="R" (GOTO :3)
IF "%RB_OR_SD%"=="S" (GOTO :4)
ELSE (GOTO :3)
:3
cls
ECHO The system will now reboot
ECHO.
pause
GOTO :MENU
:4
cls
ECHO The system will now shutdown
ECHO.
pause
GOTO :MENU
GOTO :EOF

此脚本似乎工作正常,直到用户输入 1、2、3、4、R 或 S 以外的任何内容。

如果INPUTCHOICES任何错误的符号只会导致"系统找不到指定的批次标签">消息,提示重新加载。

但是,如果将 R 或 S 以外的任何内容放入RB_OR_SD脚本只会执行下一个可用的批处理标签,这绝对是不行的。

我想用一个符号来限制用户输入,该符号来自INPUTCHOICES1234范围,以及RB_OR_SDRS范围。

最好使用户输入不区分大小写。

任何帮助将不胜感激。

我不完全确定我是否理解您要实现的目标,但我认为这可能会对您有所帮助:

:MENU
CLS
SETLOCAL
SET QUIT=FALSE
SET /P INPUTCHOICES=Type 1, 2, 3 or 4 and press ENTER:
CALL :VALIDATOR
ENDLOCAL
:validator
call :check 1
call :check 2
call :check 3
call :check 4
call :check "R"
call :check "S"
GOTO MENU
:check
IF %INPUTCHOICES% == %~1 ( CALL :EXECUTECHOICES %INPUTCHOICES% )
exit /b 0

只是限制自己回答你的问题,而不对你的代码进行任何更改/编辑......

使用findstr检查输入是否:

if [length == 1]  /be [begin/end]  with one number [1,2,3,4] and operator || == return non 0
<小时 />
setlocal EnableDelayedExpansion
:menu
cls & setlocal & set "_quit=false"
set /p "_input=type 1, 2, 3 or 4 and press enter: " || goto :menu
<con: echo%_input%| 2>nul %__AppDir__%findstr.exe/be [1-4] >nul || goto :menu
the command here only execute if findstr command return 0

如果输入fail goto :menu如果输入不在游侠1-4 goto :menu

类似的你可以用r b case-insensitive

setlocal EnableDelayedExpansion
:menu
cls & setlocal & set "_quit=false"
set /p "_input=type r or s and press enter: " || goto :menu
<con: echo%_input%| 2>nul %__AppDir__%findstr.exe/be "r s" >nul || goto :menu
the command here only execute if findstr command return 0
<小时 />
@ECHO OFF
CLS
cd /d "%~dp0"
SetlocaL EnableDelayedExpansion
:MENU
SET "QUIT=FALSE"
set "INPUTCHOICES="
2>nul (
SET /P "INPUTCHOICES=Type: 1, 2, 3 or 4 & press [ENTER] "
) || goto :MENU 
2>nul (
echo!INPUTCHOICES!| findstr /rbe [1-4] >nul
) || goto :MENU 
for /l %%i in (1 1 4)do if %%i equ !INPUTCHOICES! CALL :%%i
ENDLOCAL
GOTO :EOF
:1
cls
set "RB_OR_SD="
2>nul (
SET /P "RB_OR_SD=Type R to reboot or type S to shutdown your PC when done, and press ENTER: "
) || goto :1
2>nul (
echo!RB_OR_SD!| findstr /be "R S" >nul
) || goto :1
ECHO Performing task 1
ECHO.
pause
IF "%RB_OR_SD%"=="R" (GOTO %:^3)else GOTO %:^4
:2
cls
set "RB_OR_SD="
2>nul (
SET /P "RB_OR_SD=Type R to reboot or type S to shutdown your PC when done, and press ENTER: "
) || goto :2
2>nul (
echo!RB_OR_SD!| findstr /be "R S" >nul
) || goto :2
ECHO Performing task 2
ECHO.
pause
IF "%RB_OR_SD%"=="R" (GOTO %:3)else GOTO %:4
:3
cls
ECHO The system will now reboot
ECHO.
pause
GOTO :MENU
:4
cls
ECHO The system will now shutdown
ECHO.
pause
GOTO :MENU

相关内容

  • 没有找到相关文章

最新更新