batch choice command will not work - batch-file

I have this question about why this choice command won't work. I've looked on this site and compared all my scripting and I just can't figure out why it won't work
http://www.computerhope.com/issues/ch001674.htm
#ECHO OFF
:START
echo 1 to quit or 2 to print hello and go back to this screen
CHOICE /C:12 /N
IF ERRORLEVEL ==1 GOTO QUIT
IF ERRORLEVEL ==2 GOTO HELLO
GOTO :START
:QUIT
EXIT
:HELLO
ECHO hello
GOTO :END
:END

I've made a couple of changes and removed unnecessary code.
#ECHO OFF
:START
CLS
ECHO=1 to quit or 2 to print hello and go back to this screen
CHOICE /C 12 /N
IF ERRORLEVEL 2 (CALL :HELLO & GOTO START)
EXIT /B
:HELLO
ECHO=hello
TIMEOUT 2 1>NUL

Simpler:
#ECHO OFF
:START
echo 1 to quit or 2 to print hello and go back to this screen
CHOICE /C 12 /N
GOTO OPTION-%ERRORLEVEL%
:OPTION-1 Quit
EXIT
:OPTION-2 Hello
ECHO hello
GOTO START

Testing on the errorlevel is done wrong.
There are two possibilities:
#ECHO OFF
:BEGIN
ECHO 1 to quit or 2 to print hello and go back to this screen
CHOICE /C:12 /N
IF ERRORLEVEL 2 GOTO HELLO
IF ERRORLEVEL 1 EXIT /B
GOTO BEGIN
:HELLO
ECHO hello
GOTO BEGIN
For full details see the chapter about CHOICE in my answer on: How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
The batch user must press either 1 or 2 as otherwise the batch execution is not continued. So testing for the exit code can be done from highest to lowest with:
if errorlevel X ...
That means IF the exit code assigned to the dynamic variable errorlevel is greater OR equal X THEN execute the command (or command block).
The advantage of using this syntax is that it even works in a command block without the need to use delayed variable expansion.
The second possibility is:
#ECHO OFF
:BEGIN
ECHO 1 to quit or 2 to print hello and go back to this screen
CHOICE /C:12 /N
IF %ERRORLEVEL% == 1 EXIT /B
IF %ERRORLEVEL% == 2 GOTO HELLO
GOTO BEGIN
:HELLO
ECHO hello
GOTO BEGIN
By explicitly referencing the dynamic variable ERRORLEVEL, here with expansion before IF evaluates the condition, the order of the errorlevel checks does not matter anymore.
The disadvantage of this method is the need of using delayed expansion if CHOICE and the errorlevel evaluating conditions are within a command block defined with ( ... ).
Run in a command prompt window if /? and set /? for help about right usage of the commands IF and SET respectively get information about delayed variable expansion.
It is possible to use START as label, but it is not advisable to do so because of START is an internal command of Windows command processor. You get troubles on finding START meaning the label and START meaning the command when your batch file will use ever also the command START. BEGIN is used as label for that reason.
It is also advisable to use command EXIT always with parameter /B at least during developing a batch file to exit only the batch processing, but do not completely exit the running command process independent on calling hierarchy and option used on starting cmd.exe.
It is much easier to debug a batch file by running it from within a command prompt window (cmd.exe started with option /K to keep console window open) instead of double clicking on the batch file (cmd.exe started with option /C to close on batch execution finished) on using exit /B instead of just exit as the command prompt window keeps open. Run in a command prompt window cmd /? for details about the options of the Windows Command Processor.
GOTO BEGIN after the two errorlevel evaluations is only executed on user pressing Ctrl+C or Ctrl+Break on this prompt and presses on prompt output by cmd to terminate the batch job the key N. That results in an exit of CHOICE with exit code 0.
It would be better to use %SystemRoot%\System32\choice.exe instead of just CHOICE if the batch file is for Windows Vista or Windows Server 2003 or newer Windows versions with support of Windows command CHOICE.

Related

batch file to rename folders goto command loop the rest of the code [duplicate]

I'm trying to understand where in the code exactly does GOTO :EOF return to?
Here is the code:
SET count=1
FOR /f "tokens=*" %%G IN (somefile.txt) DO (call :subroutine "%%G")
GOTO :EOF
:subroutine
echo %count%:%1
set /a count+=1
GOTO :EOF
:EOF is a predefined label as Microsoft explains in documentation for command GOTO. The help output by running in a command prompt window goto /? explains also this special label for End Of File. But this predefined label is supported only with command extensions being enabled as by default.
The help output by running in a command prompt window call /? and of course also the documentation for command CALL explain both that goto :EOF should be used to exit a subroutine called with call :Label.
A subroutine is nothing else than another batch file embedded within current batch file called with command call. If the subroutine is at end of the batch file, real end of file marks the end of the subroutine.
But there can be multiple subroutines in a batch file.
So a command is needed for command interpreter to exit the subroutine on reaching a specific line in command processing and go back to the calling command line. goto :EOF as well as exit /B can be both used everywhere to either exit a subroutine or exit the current batch file processing.
In batch code in question the first goto :EOF is needed to exit batch file processing without an unwanted fall through to the subroutine code after finishing the loop.
The second goto :EOF in batch code of questioner is for exiting the subroutine and continue processing in FOR loop in second line. It does not exit processing of the batch file, it exits only the processing of the subroutine.
Note 1: goto EOF without a colon requires that there is really a line starting with :EOF in the batch file, i.e. the label EOF must exist in the file. goto :EOF always results in exiting subroutine/batch processing with command extensions enabled even if there is a label EOF in the batch file because of a line starting with :EOF.
Note 2: Command EXIT without parameter /B results always in exiting entire command process independent on calling hierarchy and independent on how the Windows command processor was started – with parameter /K to keep cmd.exe running as used when opening a command prompt window or with /C to close after command processing finished as used on double clicking a batch file. Therefore exit without /B should be used wisely in a batch file (best: never).
Note 3: exit /B without or with an exit code works always, but outputs an error message with command extensions disabled as demonstrated by this code:
#echo off
setlocal DisableExtensions
echo Use command exit /B with command extensions disabled.
exit /B 5
echo This line is not processed anymore.
Executing this batch file from within a command prompt window results in output of the error message:
The system cannot find the batch label specified - EOF
But the processing of the batch file is exited nevertheless with the exit code value 5 as it can be seen on running next in same command prompt window echo ERRORLEVEL is: %ERRORLEVEL% which outputs: ERRORLEVEL is: 5
It looks like there is assigned first the specified exit code value 5 to the dynamic variable ERRORLEVEL on using exit /B 5 and next is executed goto :EOF because of option /B. That fails because of disabled command extensions resulting in the error message and in exiting the batch file processing as it always occurs on a label to go to does not exist in a batch file.
In other words exit /B without or with an additional exit code always works, but there should be appended 2>nul to suppress the error message on command extensions disabled, i.e. use exit /B 2>nul (without exit code) or exit /B 5 2>nul (with exit code)
Note 4: ERRORLEVEL is not affected by goto :EOF, but the Microsoft GOTO documentation is mute on this topic. exit /B # sets ERRORLEVEL to # as documented by Microsoft. exit /B # can be also used instead of goto :EOF to exit a subroutine with a specific exit code evaluated on the command line calling the subroutine like on using the operators && or || or on next command after calling command line with if errorlevel X. However, explicitly exiting a batch file or a subroutine with a specific exit code is usually not needed as neither goto :EOF nor exit /B modify the current value of ERRORLEVEL.
Note 5: Do not use goto:EOF or call:Label in a batch file with no space between command GOTO respectively CALL (argument 0) and the label (argument 1). There should be always used goto :EOF and call :Label with a space as argument strings separator between command and label. The reason is that goto:EOF results in the attempts to find in current directory first a file with name goto: and next a file with name goto:EOF. The incorrect command call:Label results in searching for a file with name call: and next with name call:Label. The file system returns for both syntactically wrong commands twice to cmd.exe that the name is invalid. Then cmd.exe detects the colon as reason for the invalid name and splits the command up into command and label argument and finally runs the command with success. The usage of goto :EOF and call :Label does not cause any wrong file system accesses as cmd.exe immediately recognizes the string goto respectively call as internal command.
For details on ERRORLEVEL behavior see:
What are the ERRORLEVEL values set by internal cmd.exe commands?
Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
Single line with multiple commands using Windows batch file
GOTO :EOF is functionally equivalent to exit /B, but both forms only works when Extensions are enabled. The test for this point is very simple:
setlocal DisableExtensions
goto :EOF
Compare previous code vs. this one:
setlocal DisableExtensions
exit /B
This means that GOTO :EOF returns to the same point where exit /B would return.
:eof means "End of file". It's used to make the script finish without executing any of the commands below.
As the GOTO and CALL are using the same functionality to find a label you have an option to access the :EOF with CALL too:
CALL ::EOF
Same as GOTO (when CALL with the additional semicolon) the actual end-of-file will be the preferable point where the flow of the script will go. If you have your own label\function defined with EOF you can access it with single semicolon.
Though calling the :EOF has no much use - you can't put a code after the end of file so this line is actually doing nothing (though this will hit the performance as the end of file is parsed). And as GOTO and EXIT /B this wont work without enabled extensions.

How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?

If I have the following example code:
#echo off
:menu
cls
echo 1. win
echo 2. lose
set /p menu=
goto %menu%
pause>nul
:win
cls
echo yay lolz
pause>nul
:lose
cls
echo really?
pause>nul
How do I stop the batch from quitting if I type "test" instead of a valid response?
1. Documentations for the Windows commands
I suggest bookmarking in your browser:
Microsoft's command-line reference
An A-Z Index of the Windows CMD command line (SS64)
There can be get help for each Windows command by running in a command prompt window the command with /? as parameter, for example if /?, set /?, ... The execution of help results in an output of an incomplete list of Windows commands with a brief description.
2. Usage of SET /P for user prompts
It is advisable not using set /p if the user should choose from one of several offered options. There are multiple facts which must be taken into account on prompting a user for entering a string and assigning it to an environment variable:
The environment variable MyVar is not modified on usage of set /P "MyVar=Your choice: " if the user presses intentionally or by mistake just RETURN or ENTER. This means if the environment variable MyVar is not defined already before the user prompt, it is still not defined after the user prompt finished with just hitting key RETURN. And if MyVar is defined already before the user prompt, it keeps its value unmodified in case of the user presses just RETURN or ENTER. The command SET exits with error value 1 on user did not enter a string at all as documented by What are the ERRORLEVEL values set by internal cmd.exe commands?
The user has the freedom to type any string on being prompted with set /P. The batch file author has no control on what the user really enters. So the batch file author must take into account that the user enters by mistake or intentionally a string which could result in an exit of batch file execution because of a syntax error, or it does something completely different as it is defined for.
A simple example:
#echo on
:MainMenu
#set /P "menu=Your choice: "
if %menu% == 1 goto Label1
if %menu% == 2 goto Label2
goto MainMenu
:Label1
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
This batch file with echo on instead of echo off at top is started from within a command prompt window for debugging purposes.
Just RETURN is pressed on user prompt on first run. Windows command interpreter exits the batch file processing because first IF condition is preprocessed before execution of IF command to:
if == 1 goto Label1
There is obviously missing the first argument. cmd.exe encounters this syntax error and exits batch processing with an appropriate error message. The reason is the missing definition of environment variable menu which is not defined before user prompt and is still not defined after user prompt.
The string 2 is entered on second run of the batch file from within command prompt window and the batch file works as expected.
On third run of the batch file from within same command prompt window again just RETURN is pressed on user prompt. The batch file outputs again the second message. Why? The environment variable menu is still defined from second batch file execution with that string and the variable was not modified on pressing RETURN.
Okay, let us modify the example batch file to:
#echo on
:MainMenu
#set "menu=2"
#set /P "menu=Your choice: "
if "%menu%" == "1" goto Label1
if "%menu%" == "2" goto Label2
goto MainMenu
:Label1
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
This is already better as now environment variable menu is always predefined with value 2. So if the user enters nothing, a jump to Label2 is done. Also the value of previous run of variable menu has no effect anymore on execution of the batch file.
Another solution would be making use of exit code 1 on user not entering a string at all and define only in this case the environment variable with a default value by using:
#set /P "menu=Your choice: " || set "menu=2"
Single line with multiple commands using Windows batch file describes the conditional execution operator || to run the second command set "menu=2" only if the first executed prompt command set /P "menu=Your choice: " exits with an exit code not equal 0 as done when the user does not enter anything at all.
Thanks aschipfl for this contribution.
But is that really secure and fail safe now?
No, it isn't. The user still can enter by mistake a wrong string.
For example the user enters by mistake " instead of 2 which is easy on German keyboards as CapsLock+2 or Shift+2 results in entering ". The first IF command line after preprocessing is now:
if """ == "1" goto Label1
And this is again an invalid command line resulting in an exit of batch file processing because of a syntax error.
Let us assume a user enters on prompt the string:
" == "" call dir "%USERPROFILE%\Desktop" & rem
Note: There is a space at end.
The first IF condition is preprocessed by Windows command interpreter to:
if "" == "" call dir "%USERPROFILE%\Desktop" & rem " == "1" goto Label1
It can be seen that the batch file executes now a command not written in the batch file at all on both IF conditions.
How to get a user prompt fail safe and secure?
A user prompt can be made fail safe and secure by using delayed variable expansion at least for the code evaluating the string input by the user.
#echo on
:MainMenu
#setlocal EnableDelayedExpansion
#set "Label=MainMenu"
#set /P "menu=Your choice: " || set "menu=2"
if "!menu!" == "1" set "Label=Label1"
if "!menu!" == "2" set "Label=Label2"
endlocal & goto %Label%
:Label1
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
Now the user input string does not modify anymore the command lines executed by Windows command processor. So an exit of batch file processing because of a syntax error caused by user input is not possible anymore (fail safe). Furthermore, the batch file never executes commands not written in batch file (secure).
3. Usage of CHOICE for a choice prompt
There is a better command than set /P for a simple choice menu – CHOICE.
#echo off
:MainMenu
cls
echo/
echo 1 ... Option 1
echo 2 ... Option 2
echo E ... Exit
echo/
%SystemRoot%\System32\choice.exe /C 12E /N /M "Your choice: "
if errorlevel 3 exit /B
if errorlevel 2 goto Label2
if not errorlevel 1 goto MainMenu
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
The user has no freedom anymore to enter something not defined by batch file author. The batch file continues immediately after the user has pressed either 1, 2, E or Shift+E. Everything else is ignored by choice with exception of Ctrl+C.
The dynamic variable ERRORLEVEL has with three options nearly always a value in range 1 to 3 after choice terminated with returning 1 to 3 as exit code to calling cmd.exe. The exception is the rare use case that the user of a batch file pressed Ctrl+C on prompt and answers the next prompt Terminate batch job (Y/N)? of cmd.exe with N. In this case the dynamic variable ERRORLEVEL has the value 0 which is the reason for if not errorlevel 1 goto MainMenu to handle also this very special use case.
Note: if errorlevel X means IF GREATER OR EQUAL X. So it is always necessary to start with highest possible exit code of command choice.
As the exit code assigned to ERRORLEVEL is well known, it is possible on larger menus to optimize the code further by using appropriate labels:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ERRORLEVEL="
:MainMenu
cls
echo/
echo 1 ... Option 1
echo 2 ... Option 2
echo E ... Exit
echo/
%SystemRoot%\System32\choice.exe /C 12E /N /M "Your choice: "
goto Label%ERRORLEVEL%
:Label0
rem The user pressed Ctrl+C and on next prompt N and
rem so made no choice. Prompt the user once again.
goto MainMenu
:Label1
#echo Option 1 was chosen, fine.
exit /B
:Label2
#echo Option 2 was chosen, okay.
exit /B
:Label3
The usage of command CHOICE can make choice menus very simple to code.
The third command line makes sure that there is not defined by chance an environment variable with name ERRORLEVEL which would otherwise prevent accessing the current value of dynamic variable ERRORLEVEL using with the exit code of command CHOICE using the syntax %ERRORLEVEL%.
Note: The usage of goto Label%ERRORLEVEL% is only possible with the choice menu command lines not being inside a command block starting with ( and ending with a matching ).
See also: How can I make an "are you sure" prompt in a Windows batch file?
Hint 1: There is a beep sound output by CHOICE if the user presses a not acceptable key. It is not possible to suppress that beep as there is no option offered by CHOICE to avoid output of the beep sound.
Hint 2: See also my answer on Where does GOTO :EOF return to? explaining also exit /B.

Goto was unexpected at this time [BATCH]

So basically I was working on my Terminal I am creating in batch and this strange error pops up for a second and then the window closes:
"Goto was unexpected at this time"
I have no idea what's going on. Here's my code:
#ECHO off
set codename=Nature
echo Windows Bat Terminal
echo Codename "%codename%"
:terminal
set /p terminalcommand=Command:
if %terminalcommand%==help goto help
if %terminalcommand%==clr goto clear
if %terminalcommand%==exit goto exit
if %terminalcommand%==color goto color
if %terminalcommand%==time goto timedate
echo.
echo Bad command!
:terminal1
goto terminal`
To recreate simply run this in CMD.
You haven't told us what entry you made to generate this behaviour. The standard cure is to quote each side if the if comparison (if /i "%terminalcommand%"=="time" goto ... (the /i make the comparison case-insensitive)) because if you simply press enter then the command is resolved to "if ==time goto ..." and cmd will see goto where it expects a comparison operator like ==, hence the response. If you run this batch from the command prompt, the window won't close and you'll be able to see these messages better

Where does GOTO :EOF return to?

I'm trying to understand where in the code exactly does GOTO :EOF return to?
Here is the code:
SET count=1
FOR /f "tokens=*" %%G IN (somefile.txt) DO (call :subroutine "%%G")
GOTO :EOF
:subroutine
echo %count%:%1
set /a count+=1
GOTO :EOF
:EOF is a predefined label as Microsoft explains in documentation for command GOTO. The help output by running in a command prompt window goto /? explains also this special label for End Of File. But this predefined label is supported only with command extensions being enabled as by default.
The help output by running in a command prompt window call /? and of course also the documentation for command CALL explain both that goto :EOF should be used to exit a subroutine called with call :Label.
A subroutine is nothing else than another batch file embedded within current batch file called with command call. If the subroutine is at end of the batch file, real end of file marks the end of the subroutine.
But there can be multiple subroutines in a batch file.
So a command is needed for command interpreter to exit the subroutine on reaching a specific line in command processing and go back to the calling command line. goto :EOF as well as exit /B can be both used everywhere to either exit a subroutine or exit the current batch file processing.
In batch code in question the first goto :EOF is needed to exit batch file processing without an unwanted fall through to the subroutine code after finishing the loop.
The second goto :EOF in batch code of questioner is for exiting the subroutine and continue processing in FOR loop in second line. It does not exit processing of the batch file, it exits only the processing of the subroutine.
Note 1: goto EOF without a colon requires that there is really a line starting with :EOF in the batch file, i.e. the label EOF must exist in the file. goto :EOF always results in exiting subroutine/batch processing with command extensions enabled even if there is a label EOF in the batch file because of a line starting with :EOF.
Note 2: Command EXIT without parameter /B results always in exiting entire command process independent on calling hierarchy and independent on how the Windows command processor was started – with parameter /K to keep cmd.exe running as used when opening a command prompt window or with /C to close after command processing finished as used on double clicking a batch file. Therefore exit without /B should be used wisely in a batch file (best: never).
Note 3: exit /B without or with an exit code works always, but outputs an error message with command extensions disabled as demonstrated by this code:
#echo off
setlocal DisableExtensions
echo Use command exit /B with command extensions disabled.
exit /B 5
echo This line is not processed anymore.
Executing this batch file from within a command prompt window results in output of the error message:
The system cannot find the batch label specified - EOF
But the processing of the batch file is exited nevertheless with the exit code value 5 as it can be seen on running next in same command prompt window echo ERRORLEVEL is: %ERRORLEVEL% which outputs: ERRORLEVEL is: 5
It looks like there is assigned first the specified exit code value 5 to the dynamic variable ERRORLEVEL on using exit /B 5 and next is executed goto :EOF because of option /B. That fails because of disabled command extensions resulting in the error message and in exiting the batch file processing as it always occurs on a label to go to does not exist in a batch file.
In other words exit /B without or with an additional exit code always works, but there should be appended 2>nul to suppress the error message on command extensions disabled, i.e. use exit /B 2>nul (without exit code) or exit /B 5 2>nul (with exit code)
Note 4: ERRORLEVEL is not affected by goto :EOF, but the Microsoft GOTO documentation is mute on this topic. exit /B # sets ERRORLEVEL to # as documented by Microsoft. exit /B # can be also used instead of goto :EOF to exit a subroutine with a specific exit code evaluated on the command line calling the subroutine like on using the operators && or || or on next command after calling command line with if errorlevel X. However, explicitly exiting a batch file or a subroutine with a specific exit code is usually not needed as neither goto :EOF nor exit /B modify the current value of ERRORLEVEL.
Note 5: Do not use goto:EOF or call:Label in a batch file with no space between command GOTO respectively CALL (argument 0) and the label (argument 1). There should be always used goto :EOF and call :Label with a space as argument strings separator between command and label. The reason is that goto:EOF results in the attempts to find in current directory first a file with name goto: and next a file with name goto:EOF. The incorrect command call:Label results in searching for a file with name call: and next with name call:Label. The file system returns for both syntactically wrong commands twice to cmd.exe that the name is invalid. Then cmd.exe detects the colon as reason for the invalid name and splits the command up into command and label argument and finally runs the command with success. The usage of goto :EOF and call :Label does not cause any wrong file system accesses as cmd.exe immediately recognizes the string goto respectively call as internal command.
For details on ERRORLEVEL behavior see:
What are the ERRORLEVEL values set by internal cmd.exe commands?
Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
Single line with multiple commands using Windows batch file
GOTO :EOF is functionally equivalent to exit /B, but both forms only works when Extensions are enabled. The test for this point is very simple:
setlocal DisableExtensions
goto :EOF
Compare previous code vs. this one:
setlocal DisableExtensions
exit /B
This means that GOTO :EOF returns to the same point where exit /B would return.
:eof means "End of file". It's used to make the script finish without executing any of the commands below.
As the GOTO and CALL are using the same functionality to find a label you have an option to access the :EOF with CALL too:
CALL ::EOF
Same as GOTO (when CALL with the additional semicolon) the actual end-of-file will be the preferable point where the flow of the script will go. If you have your own label\function defined with EOF you can access it with single semicolon.
Though calling the :EOF has no much use - you can't put a code after the end of file so this line is actually doing nothing (though this will hit the performance as the end of file is parsed). And as GOTO and EXIT /B this wont work without enabled extensions.

Call batch within batch?

How do I call multiple batch files within a single batch? When I try it always goes to the same one or none at all and closes window.
#echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.
SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT
:1
call %userprofile%\desktop\\Menu1.bat
:2
call %userprofile%\desktop\Menu2.bat
There are several issues with provided batch code in question.
The first one is that after processing of the batch file called with command CALL finished, the processing of current batch file continues with the next command respectively line, except the called batch file contains itself the command EXIT without parameter /B as in this case the command processor terminates itself independent on calling hierarchy.
For details about CALL behavior see answers on:
How to call a batch file in the parent folder of current batch file?
In a Windows batch file, can you chain-execute something that is not another batch file?
The second issue is that folder path assigned to environment variable USERPROFILE could contain 1 or more spaces (default on Windows 2000/XP, possible on later Windows versions depending on user name). Therefore always enclose a string referencing USERPROFILE or USERNAME in double quotes.
The third and most difficult to handle issue is that the user of a batch file on prompt with set /P has the freedom to enter anything and not just what the writer of the batch file suggests.
For example
SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
results in an exit of batch processing caused by a syntax error if the batch user hits just RETURN or ENTER without entering anything at all and the environment variable choice is not already defined with a useful string because in this case the next line to process by command processor is:
IF ==1 GOTO 1
It is good practice to define the environment variable with a default value before set /P as this value is kept when the batch user just hits RETURN or ENTER.
A batch user has also the freedom on using set /P to enter anything including syntax critical characters like " or < or | or > and others by mistake or intentionally (for breaking batch processing by a syntax error).
Therefore it is in general better for menus in batch files to use the command choice (Microsoft article) because then the batch user can enter only what the writer of the batch file offers. But CHOICE is available only by default for Windows Server 2003 and later Windows. And there are different versions of choice (SS64 article with additional information) with a different set of options. So it depends on which Windows version(s) the batch file is designed for if CHOICE can be used at all.
It is also not good to name an environment variable or a label like a command although possible. Therefore choice is not a good name for an environment variable.
Here is a commented batch file with a code which avoids all those issues.
#echo off
:MainMenu
setlocal EnableDelayedExpansion
title MENU0
cls
echo 1 - Select Menu 1
echo 2 - Select Menu 2
echo 0 - Exit
echo.
rem Define 0 as default value in case of user just hits RETURN or ENTER.
set "UsersChoice=0"
set /P "UsersChoice=Type the number or letter of task you want, then press ENTER: "
rem Has the user really entered just one of the offered characters?
rem There must be nothing to process if the user has entered just 0
rem or 1 or 2. Otherwise the user's choice was either by mistake or
rem intentionally entered wrong. The string entered by the user is
rem referenced with delayed expansion to avoid an exit of batch
rem processing in case of user entered a syntax critical character.
for /F "tokens=1 delims=012" %%I in ("!UsersChoice!") do (
endlocal
goto MainMenu
)
rem Now it is safe to reference the variable value without usage of delayed
rem expansion as a syntax error caused by user input can't occur anymore.
rem The entered string does not contain any not expected character. But
rem it is possible that for example 11 was entered by mistake instead
rem of just 1. The entered string should have a length of 1 character.
if not "%UsersChoice:~1,1%" == "" (
endlocal
goto MainMenu
)
rem Exit this batch processing on user entered 0. Previous environment is
rem automatically restored by command processor by an implicit endlocal.
if "%UsersChoice%" == "0" exit /B
rem Restore previous environment as the called batch files are most
rem likely written for using standard command environment with delayed
rem expansion not enabled (exclamation mark interpreted different).
rem The current value of local environment variable must be passed
rem to previous environment for usage on GOTO command.
endlocal & goto Menu%UsersChoice%
:Menu1
call "%USERPROFILE%\Desktop\Menu1.bat"
goto MainMenu
:Menu2
call "%USERPROFILE%\Desktop\Menu2.bat"
goto MainMenu
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.
call /?
cls /?
echo /?
endlocal /?
exit /?
for /?
goto /?
rem /?
set /?
setlocal /?
title /?
For meaning of & in line endlocal & goto Menu%UsersChoice% see answer on Single line with multiple commands using Windows batch file.
I tried your code and what I found was that when the input was 1 both :1 and :2 are executed but when the input is 2 only :2 is executed. To fix this you need to specify the end of :1 using Exit or another goto.
You might see that none the batches are being executed IF you do not put a pause in the end of your script. They would be executed but the result might just flash out of the screen.
Also I do not understand why have you used \\Menu1.batand not \Menu1.bat in
:1
call %userprofile%\desktop\\Menu1.bat
The final working code for me-
#echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.
SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT
:1
call yourpathhere\Menu1.bat
pause
GOTO cont
:2
call whatsoever\Menu2.bat
pause
GOTO cont
:cont
exit
That should fix your problem.
Hope I helped.
I may not be a pro, but I could help you!
I always add extra code on my games in order to avoid bugs, like this:
set /p letter=
if %letter% == 1 goto nocheck1
if %letter% == 2 goto nocheck2
if %letter% == 3 exit
:nocheck1
if %letter% == 1 goto saves
:nocheck2
if %letter% == 2 goto howtoplay
Maybe it could work on your problem!
I might have the code to do it:
#echo off
cls
:menu
cls
echo 1. Open Batch 1
echo 2. Open Batch 2
set /p test=Enter number here ----->
if %test% == 1 goto check1
if %test% == 2 goto check2
Edit the "Batch file name" text with your location of your batch file.
:check1
if %test% == 1 start C:\Users\%username%\Desktop\(batch file name).bat
:check2
if %test% == 2 start C:\Users\%username%\Desktop\(batch file name).bat
If there's still any errors with my code, let me know.
Hope this helps your problem!
Use cd to go to the location of batch file. For example:
rem myscript
echo calling batch file
cd demo\desktop\script
execute.bat
echo done
After the execution of that batch, control will return to the next line of your script.
Use "Start" instead of "Call" like so,
#echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.
SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT
:1
start %userprofile%\desktop\\Menu1.bat
:2
start %userprofile%\desktop\Menu2.bat
Try This:
#echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.
SET /P choice=Type the number or letter of task you want, then press
Enter:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT
:1
cd users
cd %userprofile%
cd desktop
:: call Menu1.bat or use: start Menu1.bat
:: exit
:2
cd users
cd %userprofile%
cd desktop
:: call Menu2.bat or use: start Menu2.bat
:: exit
start "" C:\location\of\file\file.bat
This opens a new window, and as long as you have more commands to follow, the previous file that is calling the new one will still run along with this one.

Resources