Reputation: 35
I try to make a batch file which asks the user a question (just as a test for if and else statements) and waits for the answer input by the user.
I tried to use:
@echo off
echo hi!
pause
CLS
echo how are you?
set /p what=
if %what%==Good (
GOTO good
) else (
GOTO what )
:Good
CLS
echo cool!
pause
GOTO end
:what
CLS
echo what?
pause
GOTO end
:end
The goal was to go to label what
and continue there the batch file processing on user inputs anything other than Good
.
But if nothing is entered by the user because of pressing only ENTER, the execution of the batch script just ends rather than going to code below label what
. Also if some other things with spaces are typed on prompt, the batch file just ends the script as well.
Why does batch file execution end unexpected on entering nothing on a user prompt?
Upvotes: 1
Views: 523
Reputation: 49084
set /P
prompts the user for a string. The user has the freedom
enter bad strings like
a == a rd /s /q "%TEMP%" & rem
The last example results with your batch code in deletion of the user's folder for temporary files. So please don't try that user input with your batch file.
When the user enters nothing at all on prompt, the specified environment variable keeps its current value. In your case with environment variable what
not defined at all on first run, it is still not defined after the user prompt and therefore the next command line executed by cmd.exe
after preprocessing the command line is:
if ==Good (
So the left operand is missing and Windows command interpreter exits the execution of the batch file because of this syntax error as it can be seen on running this batch file from within a command prompt window instead of double clicking on the batch file. See also Debugging a batch file.
To make such a command prompt safe against no input or even bad input, use the following code:
@echo off
cls
echo Hi!
echo/
set "UserInput="
set /P "UserInput=How are you? "
if not defined UserInput goto WrongInput
setlocal EnableDelayedExpansion
if /I "!UserInput!" == "Good" endlocal & set "UserInput=" & goto Good
endlocal
set "UserInput="
:WrongInput
echo/
echo What?
echo/
pause
goto :EOF
:Good
echo/
echo Cool!
echo/
pause
First the environment variable UserInput
is definitely deleted before prompting the user.
After the user prompt an IF condition is used to check if the variable UserInput
is still not defined which means the user has not input anything at all if that condition is true.
But if the user has entered a string, a new local environment is setup with enabled delayed environment variable expansion, and the value of UserInput
is compared case-insensitive with the string "Good"
using delayed environment variable expansion. This prevents on accidentally or intentionally wrong input that the batch file execution exits due to a syntax error caused by the user input or do something completely different than being designed for.
In this case the local environment with enabled delayed environment variable expansion is no longer needed after string comparison which always includes also the double quotes around the strings on both sides. Therefore the previous environment is immediately restored in any case.
Well, it is valid to omit if not defined UserInput
because of using surrounding double quotes on the two compared strings because the left string can't be empty anymore. And in general it is advisable to define a local environment for a batch file in case of being ever called later from other batch files or from within a command prompt window.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
echo Hi!
echo/
set "UserInput="
set /P "UserInput=How are you? "
setlocal EnableDelayedExpansion
if /I "!UserInput!" == "Good" endlocal & goto Good
endlocal
echo/
echo What?
echo/
pause
goto EndBatch
:Good
echo/
echo Cool!
echo/
pause
:EndBatch
endlocal
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.
cls /?
echo /?
endlocal /?
goto /?
if /?
pause /?
set /?
setlocal /?
See also following questions with their answers for even more details and important facts to know on using environment variables within a batch file:
Upvotes: 1