The daily "menu" question.
Two items with your code as it stands:
If you use parenthesised code (code blocks
) in an if
statement, then each (
must be balanced by a )
. You have many more (
than )
so your if
statement will fail as it is invalid syntax.
If you press just Enter then the variable (in this case, menu
) remains unchanged, it is not automatically set
to nothing.
It's very possible that menu
will in fact be empty when you start the batch, but to make sure, put
set "menu="
before the set /p
statement.
BUT the downside of this is that if menu
is empty when the if
is processed, for instance,
if %menu%==2 (
will be interpreted as
if ==2 (
which is invalid syntax.
The classic cure, generally, is to code
if "%menu%"=="2" (
where the quotes are both included in the comparison and partially-sanitise the input so that a value that contains spaces will now be correctly interpreted.
BUT (again) there are problems. The input is uncontrolled, so you could enter "hello"
or hello
or %hello%
or he"llo
, which will all cause problems with the if
statement, even if the if "%menu%"=="2"
syntax is used.
Hence, the solution is to use choice
- there are many examples available on SO - just use the search
facility.
Also please consider your code structure and logic. A long cascaded if/then
sequence is a nightmare.
Consider
if "%menu%"=="2" (goto somewhere)
rem MENU must be "not 2" to reach here
if "%menu%"=="3" (goto somewhereelse)
rem MENU must be "neither 2 nor 3" to reach here
if "%menu%"=="4" (goto somewhereelseagain)
rem MENU must be "neither 2,3 nor 4" to reach here
But really, you're far better off with choice
.