2

I need the choice command to do the same thing as this block of code:

set /p start= Select Mode: if %start% == 1 goto money if %start% == 2 goto payouts if %start% == 3 goto tutorial if %start% == 4 exit /b 

I have been trying for quite some time now and cannot figure it out Thanks in advance!

3
  • 2
    Use the %errorlevel% variable when using the CHOICE command. Commented Mar 10, 2017 at 15:02
  • 1
    From the HELP file for the CHOICE command: The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. Commented Mar 10, 2017 at 15:21
  • See the answer on: How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? There is explained in full details with examples the use of set /P and of choice with their advantages and disadvantages. There is also explained how to evaluate the exit code of choice most efficient in various use cases. Commented May 22 at 17:12

4 Answers 4

7

The simplest solution is not check the value of errorlevel at all, but directly use it in a goto command. In this way, you avoid the usual series of if commands, so the code is simpler.

choice /C 1234 /M "Select Mode: " goto option-%errorlevel% 

The rest of the code shoud be like this:

:option-1 money echo Money goto loop :option-2 payouts echo Payouts goto loop :option-3 tutorial echo Tutorial goto loop :option-4 exit /B 
Sign up to request clarification or add additional context in comments.

Comments

5

Using the command choice like this

choice /c 1234 /M "Select Mode: " 

upon exit, errorlevel will be set to the index (starting with 1) of the selected choice. For acting upon the errorlevel it's important to remember that "if errorlevel n" traps not only n, but also all higher values, meaning that they need to be specified in reverse

 if errorlevel 4 goto exit if errorlevel 3 goto tutorials if errorlevel 2 goto payout if errorlevel 1 goto money 

Comments

3

An alternative to the answer by fvu, is to use the %ErrorLevel% variable as Squashman suggested:

Choice /C 1234 /M "Select Mode: " If %ErrorLevel%==1 GoTo money If %ErrorLevel%==2 GoTo payouts If %ErrorLevel%==3 GoTo tutorial Exit/B [:money | :payouts | :tutorial] 

Comments

0

For just Yes/No use choice without parameters: If used without parameters, choice displays the default choices Y and N.

echo Proceed? choice if NOT %ERRORLEVEL%==1 exit /b 

This waits until the user presses Y or N and continues the batch script only if Yes was chosen, which means %ERRORLEVEL% is 1.

It exits the batch script on No and all other cases, even if choice detects an error condition and returns an ERRORLEVEL value of 255, or the user presses CTRL+BREAK or CTRL+C and choice returns an ERRORLEVEL value of 0. See also ss64.com


Edit: With /M parameter one can skip the echo. And for numeric comparisions as is here the case on can use NEQ (assuming Command Extensions are enabled, which they are probably):

choice Proceed? if %ERRORLEVEL% NEQ 1 exit /b 

5 Comments

The third line cannot be used inside a command block beginning with ( and ending with matching ) because of %ERRORLEVEL% is replaced already by the current value of the dynamic variable ERRORLEVEL on parsing the entire command block before %SystemRoot%\System32\choice.exe is executed at all. See also variables are not behaving as expected and How can I make an "are you sure" prompt in a Windows batch file?
@Mofi Great hint, thank you! Then you would need to setlocal EnableDelayedExpansion and switch to exclamation marks as variable delimiter as in !ERRORLEVEL!. I wanted a simple confirmation prompt with ability to stop the execution for a simple .bat script that restarts a service. For that I think this is as simple as it gets.
Use the following two command lines in the batch file: 1. %SystemRoot%\System32\choice.exe /C NY /N /M "Proceed [Y,N]?" 2. if ERRORLEVEL 2 exit /B 0 In a batch file should be always used %SystemRoot%\System32\choice.exe and never just choice as in this case the Windows Command Processor does not need to make several file system accesses to find the executable and it is impossible that by mistake a program or script with name choice is executed instead of the choice executable in the Windows System32 directory.
The batch file is executed faster with less file system accesses and also more fail-safe with using the fully qualified file name of choice in the batch file. The Windows command choice can exit with the command line in the comment above only with three exit codes: 1. 2 on user pressing key Y. In this case the execution of the batch file is exited with exit code 0 due to the simple if condition in the next line working always even inside a command block. 2. 1 on user pressing key N. In this case the batch file processing is continued by cmd.exe.
3. 0 on user pressing Ctrl+C or Ctrl+Break and the prompt output now by cmd.exe for exiting batch file processing is answered by the user with pressing key N. The batch file processing is continued also in this use case. choice.exe cannot exit with any other exit code like 255 as the passed parameters are definitely always 100% correct as defined in the batch file and there are not more than the two choices N and Y which are displayed in the order Y and N.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.