set /p _sure="Are you sure?"
::The underscore is used to ensure that "sure" is not an enviroment
::varible
if /I NOT "_sure"=="y" (
::the /I makes it so you can
exit /b
) else (
::Any other modifications...
)
或者如果你不想用那么多台词..。
Set /p _sure="Are you sure?"
if /I NOT "_sure"=="y" exit /b
::Any other modifications and commands.
下面是一个使用首选 选择和 choice.exe上的 SET/P的提示示例,在运行 Windows 的用过的计算机上不可用。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
echo This is an example for prompting a user.
echo/
if exist "%SystemRoot%\System32\choice.exe" goto UseChoice
setlocal EnableExtensions EnableDelayedExpansion
:UseSetPrompt
set "UserChoice="
set /P "UserChoice=Are you sure [Y/N]? "
set "UserChoice=!UserChoice: =!"
if /I "!UserChoice!" == "N" endlocal & goto :EOF
if /I not "!UserChoice!" == "Y" goto UseSetPrompt
endlocal
goto Continue
:UseChoice
%SystemRoot%\System32\choice.exe /C YN /N /M "Are you sure [Y/N]?"
if not errorlevel 1 goto UseChoice
if errorlevel 2 goto :EOF
:Continue
echo So you are sure. Okay, let's go ...
rem More commands can be added here.
endlocal
注意: 这个批处理文件使用的命令扩展名在 Windows 95/98/ME 上不可用,它使用 command.com而不是 cmd.exe作为命令解释器。
添加命令行 set "UserChoice=!UserChoice: =!"是为了在 Windows NT4/2000/XP 上使用 echo Y | call PromptExample.bat调用这个批处理文件,而不需要使用 echo Y| call PromptExample.bat。在运行两个字符串比较之前,它从从 STDIN读取的字符串中删除所有空格。
echo Y | call PromptExample.bat导致 为什么SPACE被分配到环境变量 UserChoice。这将导致在处理提示两次,因为 "Y "既不是大小写不敏感的等于 "N",也不是 "Y"而不删除首先所有的空格。因此,UserChoice以 为什么SPACE作为值将导致第二次运行提示符,选项 N在第二次提示符执行时被定义为批处理文件中的默认值,这将导致批处理文件处理意外退出。是的,安全使用 UserChoice1是非常棘手的,不是吗?
如果用户按 Ctrl+C或 Ctrl+Break,choice.exe退出 0,并接下来回答 cmd.exe输出的问题,以终止 N为 NO 的批处理作业。由于这个原因,添加了条件 if not errorlevel 1 goto UserChoice,以便用 Y或 N批处理文件代码再次提示用户在提示中获得确切的答案。感谢 拨号器提供关于这个可能的特殊用例的信息。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
echo This is an example for prompting a user.
echo/
if exist "%SystemRoot%\System32\choice.exe" goto UseChoice
setlocal EnableExtensions EnableDelayedExpansion
:UseSetPrompt
set "UserChoice="
set /P "UserChoice=Are you sure [Y/N]? "
set "UserChoice=!UserChoice: =!"
if /I not "!UserChoice!" == "Y" endlocal & goto :EOF
endlocal
goto Continue
:UseChoice
%SystemRoot%\System32\choice.exe /C YN /N /M "Are you sure [Y/N]?"
if not errorlevel 2 if errorlevel 1 goto Continue
goto :EOF
:Continue
echo So you are sure. Okay, let's go ...
endlocal
This code results in continuation of batch file processing below the batch label :Continue if the user pressed definitely key Y. In all other cases the code for N is executed resulting in an exit of batch file processing with this code independent on user pressed really that key, or entered something different intentionally or by mistake, or pressed Ctrl+C or Ctrl+Break and decided next on prompt output by cmd not terminating the batch job.
@echo off
choice /M "[Opt 1] Do you want to continue [Yes/No]"
if errorlevel 255 (
echo Error
) else if errorlevel 2 (
set "YourChoice=will not"
) else if errorlevel 1 (
set "YourChoice=will"
) else if errorlevel 0 (
goto :EOF
)
echo %YourChoice%
pause
@echo off
for /f "tokens=* delims=" %%# in ('yesnopopup.bat') do (
set "result=%%#"
)
if /i result==no (
echo user rejected the script
exit /b 1
)
echo continue
rem --- other commands --
@echo off
for /f "tokens=* delims=" %%# in ('buttons.bat "Yep!" "Nope!" ') do (
set "result=%%#"
)
if /i result==2 (
echo user rejected the script
exit /b 1
)
echo continue
rem --- other commands --
:: rem at the top of the script
setlocal enabledelayedexpansion
:: choice example
CHOICE /C YNC /M "Continue? Press Y for Yes, N for No or C for Cancel."
If /I "[!errorlevel!]" NEQ "[1]" ( GOTO START_OVER )