Introduction
So you have a code similar to this one:
@echo off
:menu
set /P "errorlevel2=Please enter your choice: "
if %errorlevel2%==s (
echo/
echo/
echo Press any key to continue...
echo/
echo/
pause>nul
start haxmenu\pin.bat
goto menu
)
First you have to take into consideration that Windows command processor replaces %errorlevel2%
with the string of this variable before the line is parsed for a command.
So it depends on existence and value of environment variable errorlevel2
how the line with the IF condition looks like on execution of the batch file.
For example with errorlevel2
not defined at all and user on prompt just hits the key RETURN or ENTER the variable errorlevel2
is still not existing after user prompt and the IF condition line looks therefore as follows:
if ==s (
This IF condition is of course invalid and command processor exits batch processing because of the syntax error.
Using double quotes around both strings to compare is a first step to avoid the syntax error.
if "%errorlevel2%"=="s" (
Now the resulting line on execution of the batch file with nothing entered by the user is:
if ""=="s" (
This is a valid syntax.
But it does not prevent your batch code completely on exiting because of a syntax error as it can be seen by entering on prompt for example a single double quote. This would result in
if """=="s" (
which results again in an exit of batch processing because of a syntax error caused by the odd number of double quotes on left side of the equal operator.
Before thinking about a solution for this additional issue, we should also think about what happens when the user really just press RETURN or ENTER on prompt without entering anything. Run the following code:
@echo off
set /P "UserInput=Enter a number: "
echo UserInput=%UserInput%
set /P "UserInput=Hit key RETURN: "
echo UserInput=%UserInput%
pause
After entering on first prompt a number like 23
and hit on second prompt just RETURN as requested, the number 23
is output also a second time.
The current value of the environment variable is not modified if the user just hits RETURN on user prompt. This should be always taken into account when using set /P
in a menu, especially if this menu is within a loop because in this case the variable has already a value after first choice made by the user which would be kept unmodified on next prompt if the user does not enter anything.
Solution 1: Remove all double quotes before string comparison
One solution is predefining the variable with a single double quote and remove all double quotes from input string.
@echo off
:menu
set "errorlevel2=""
set /P "errorlevel2=Please enter your choice: "
set "errorlevel2=%errorlevel2:"=%"
if /I "%errorlevel2%"=="s" (
echo/
echo/
echo Press any key to continue...
echo/
echo/
pause>nul
start haxmenu\pin.bat
goto menu
)
It is important that the value of variable errorlevel2
is predefined with a string containing at least one double quote for this solution if not one of the options the user should choose from is defined as default value because the variable errorlevel2
must exist with any value to avoid a syntax error on line removing all double quotes from string.
See Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation why using syntax set "variable=value"
even with value containing a double quote.
Solution 2: Using delayed expansion on string comparison
A syntax error on string comparison because of string entered by the user could be avoided by using delayed expansion because the IF condition line itself is not modified in this case by the input string.
@echo off
setlocal EnableDelayedExpansion
:menu
set "errorlevel2="
set /P "errorlevel2=Please enter your choice: "
if "!errorlevel2!"=="s" (
echo/
echo/
echo Press any key to continue...
echo/
echo/
pause>nul
start haxmenu\pin.bat
goto menu
)
endlocal
The variable errorlevel2
is deleted now always before prompting the user.
Whatever the user enters does not modify the batch code line itself and therefore the string comparison always works even with a string entered with an odd number of double quotes.
Solution 3: Using command choice for simple menus
Windows offers the command choice (Microsoft article) for tasks where a user should select one of offered options by key. Using this command is nearly always better for single character choices than using set /P
.
But how choice (SS64 article) can be used depends also on version of Windows respectively which version of choice is used as there are multiple versions with different syntax and capabilities. choice should be used for simple menus if compatibility of batch file with Windows versions prior Windows Vista is not needed anymore as the batch file is definitely never run on Windows XP or even older Windows.