Reputation: 49
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
Upvotes: 1
Views: 3452
Reputation: 38718
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
Upvotes: 1
Reputation: 49186
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.
Upvotes: 1
Reputation: 67256
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
Upvotes: 1