1

我为 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脚本中,则只执行下一个可用的批处理标签,这绝对不行。

我想用,和的范围以及.和的范围中的一个符号来限制用户输入1234INPUTCHOICESRSRB_OR_SD

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

任何帮助将不胜感激。

4

2 回答 2

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 如果输入不在 Ranger 中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
于 2020-12-21T03:31:40.360 回答
0

我不完全确定我理解你想要达到的目标,但我认为这可能会对你有所帮助:

: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
于 2018-10-16T11:05:37.040 回答