I have a batch file that automates copying a bunch of files from one place to the other and back for me. Only thing is as much as it helps me I keep accidentally selecting that command off my command buffer and mass overwriting uncommited changes.
What code would I need for my .bat file to make it say "are you sure", and make me type "y" before it ran the rest of the file? If anything but "y" is typed it should exit execution on that line.
Edit Nov 27 Ok I marked this unanswered again because I still can't figure it out. When I call "exit;" it closes cmd.exe which is not what I want. All this because Windows implemented command buffer wrong [differently than what I am used to at least]
There are two commands available for user prompts on windows command line:
/P
available on all Windows NT versions with enabled command extensions andset is an internal command of Windows command processor
cmd.exe
. The option/P
to prompt a user for a string is available only with enabled command extensions which are enabled by default as otherwise nearly no batch file would work anymore nowadays.choice.exe is a separate console application (external command) located in
%SystemRoot%\System32
.choice.exe
of Windows Server 2003 can be copied into directory%SystemRoot%\System32
on a Windows XP machine for usage on Windows XP like many other commands not available by default on Windows XP, but available by default on Windows Server 2003.It is best practice to favor usage of CHOICE over usage of SET /P because of the following reasons:
/C
(and Ctrl+C) and outputs an error beep if the user presses a wrong key.echo Y | call PromptExample.bat
on using CHOICE.cmd
because of a syntax error, or in execution of commands not included at all in the batch file on not good coded batch file. It needs some efforts to get SET /P secure against by mistake or intentionally wrong user input.Here is a prompt example using preferred CHOICE and alternatively SET /P on
choice.exe
not available on used computer running Windows.Note: This batch file uses command extensions which are not available on Windows 95/98/ME using
command.com
instead ofcmd.exe
as command interpreter.The command line
set "UserChoice=!UserChoice: =!"
is added to make it possible to call this batch file withecho Y | call PromptExample.bat
on Windows NT4/2000/XP and do not require the usage ofecho Y| call PromptExample.bat
. It deletes all spaces from string read from STDIN before running the two string comparisons.echo Y | call PromptExample.bat
results in YSPACE getting assigned to environment variableUserChoice
. That would result on processing the prompt twice because of"Y "
is neither case-insensitive equal"N"
nor"Y"
without deleting first all spaces. SoUserChoice
with YSPACE as value would result in running the prompt a second time with optionN
as defined as default in the batch file on second prompt execution which next results in an unexpected exit of batch file processing. Yes, secure usage of SET /P is really tricky, isn't it?For even more details on usage of SET /P and CHOICE for prompting user for a choice from a list of options see answer on How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Some more hints:
UserChoice
withN
andY
, but the value ofUserChoice
surrounded by"
with"N"
and"Y"
.EQU
andNEQ
are designed primary for comparing two integers in range -2147483648 to 2147483647 and not for comparing two strings.EQU
andNEQ
work also for strings comparisons, but result on comparing strings in double quotes on a useless attempt to convert left string to an integer.EQU
andNEQ
can be used only with enabled command extensions. The comparison operators for string comparisons are==
andnot ... ==
which work even with disabled command extensions as evencommand.com
of MS-DOS and Windows 95/98/ME already supported them. For more details on IF comparison operators see Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files.goto :EOF
requires enabled command extensions to really exit batch file processing. For more details see Where does GOTO :EOF return to?For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
choice /?
echo /?
endlocal /?
goto /?
if /?
set /?
setlocal /?
See also:
It explains the reason for using syntax
set "variable=value"
on assigning a string to an environment variable.if errorlevel X
behavior and operator&
.|
and handle STDIN.SystemRoot
.