Programming Field

Choice - DOS/Command Prompt Reference

Displays two or multiple choices. To select a choice, press the corresponding key.

[Windows NT series/XP] This command does not exist. However, it is possible to obtain it by installing the toolkit or bringing it in from an old MS-DOS and executing it.

[Windows Vista or later] The ‘choice.exe’ program exists and provides similar functionality, but the syntax is slightly different. (Please see the following ‘Syntax’ for details.)

Syntax

MS-DOS, Windows 95/98/Me version

choice[.com] [/C[:]<choices>] [/N] [/S] [/T[:]c,nn] [<message>]

Options

/C[:]<choices>

Specifies the characters for the choices (the colon ‘:’ can be omitted). For example, if you want to allow the choice between ‘Y’ and ‘N’, you would specify ‘/C:YN’, and if you want to allow the choice between ‘Y’, ‘N’, and ‘C’, you would specify ‘/C:YNC’.

If omitted, it defaults to ‘/C:YN’.

/N Does not display choices and ‘?’. If not specified, following <message>, choices will be displayed as ‘[Y,N]?’ (in the case of /C:YN).
/S Distinguishes between uppercase and lowercase for choices.
/T[:]c,nn Sets the time for making a choice. c is the choice when the time runs out, and nn specifies the time limit in seconds. Please specify this time limit as a one or two-digit number. The choice automatically made when time runs out will be displayed.
<message> Displays a message before ‘[Y,N]?’ (in the case of /C:YN). If not specified, no message will be displayed.

Windows Vista or later version

choice[.exe] [/C <choices>] [/N] [/CS] [/T nnnn /D c] [/M <message>]

Options

/C <choices>

Specify the characters for the choices (there should be a space between /C and the choices). For example, if you want to allow the choice between ‘Y’ and ‘N’, you would specify ‘/C YN’, and if you want to allow the choice between ‘Y’, ‘N’, and ‘C’, you would specify ‘/C YNC’.

If omitted, it defaults to ‘/C YN’.

/N Does not display choices and ‘?’. If not specified, following <message>, choices will be displayed as ‘[Y,N]?’ (in the case of /C YN).
/CS Distinguishes between uppercase and lowercase for choices.
/T nnnn

Sets the time for making a choice. nnnn will be the time limit in seconds. Please specify this time limit as a numerical value up to four digits.

To specify the choice made when time runs out, use ‘/D’. /D must be specified.

/D c Specifies the choice made when the time specified with /T elapses. /T must always be used in conjunction.
/M <message> Displays a message before ‘[Y,N]?’ (in the case of /C YN). If not specified, no message will be displayed. If the message contains spaces, it should be enclosed in double quotation marks (").

Details

Usage of Choice (Vista or later)

Choice is a program that displays choices on the screen and waits for input. While it doesn't offer additional features, it is convenient for branching in batch files as the exit code varies based on the selected character.

If a key that is not part of the choices is pressed, a beep sound will be produced (the BEL (07h) character will be output to the console).

Exit codes

This command returns an exit code (error code) when a choice is made. Choosing the first option returns 1, the second option returns 2, and choosing the nth option returns n as the exit code. Additionally, pressing Ctrl+Break or Ctrl+C during input waiting results in 0, and in case of an error, it becomes 255. When differentiating the processing based on the exit code, the If Errorlevel syntax is used. However, note that If Errorlevel branches for ‘n or higher’, so if you want to check for a specific value,

choice /C:YN Continue
if errorlevel 255 goto OnExit
if errorlevel 2 goto OnExit
if errorlevel 1 goto Next
goto OnExit

...it is necessary to arrange them in descending order as shown above. (This is not applicable when using %ERRORLEVEL%.)

Samples

Sample 1 (Batch file) [MS-DOS, Windows 95/98/Me]

@echo off
rem An example for batch files
choice /C:YN Continue
if errorlevel 2 goto OnExit
rem To handle exit code 3, it should be placed above the lines mentioned earlier.
if errorlevel 1 call work.bat

rem If N is chosen, it will jump to this section.
:OnExit

Displays choices with Choice, executes ‘work.bat’ if ‘Y’ is selected, and exits if ‘N’ is selected. This sample uses the message parameter to display the message ‘Continue’ before the choices.

Sample 2 (Batch file) [Windows Vista or later]

@echo off
rem An example for batch files
choice /C YN /M "Continue"
if errorlevel 2 goto OnExit
rem To handle exit code 3, it should be placed above the lines mentioned earlier.
if errorlevel 1 call work.bat

rem If N is chosen, it will jump to this section.
:OnExit

Displays choices with Choice, executes ‘work.bat’ if ‘Y’ is selected, and exits if ‘N’ is selected. This sample uses the /M option to display the message ‘Continue’ before the choices.

Sample 3 (Batch file) [MS-DOS, Windows 95/98/Me]

@echo off
:OnRetry
foo.exe
if not errorlevel 1 goto OnExit
echo An error has occurred.
echo - If you want to retry the process, please press R within 30 seconds.
echo - If you want to cancel the process immediately, please press C.
choice /C:RC /T:C,30 /N
if errorlevel 2 goto OnExit
if errorlevel 1 goto OnRetry

:OnExit

Prompts for a choice to retry if ‘foo.exe’ returns an exit code greater than 0. However, this batch program ensures that it doesn't remain waiting for input indefinitely by incorporating a 30-second timeout.

Sample 4 (Batch file) [Windows Vista or later] [Extensions]

@echo off
:OnRetry
foo.exe
if "%errorlevel%"=="0" goto OnExit
echo An error has occurred.
echo - If you want to retry the process, please press R within 30 seconds.
echo - If you want to cancel the process immediately, please press C.
choice /C RC /T 30 /D C /N
if "%errorlevel%"=="1" goto OnRetry

:OnExit

Prompts for a choice to retry if ‘foo.exe’ returns a non-zero exit code. However, this batch program ensures that it doesn't remain waiting for input indefinitely by incorporating a 30-second timeout.

Note that if you simply want to implement a timeout, you can use the Timeout command as well.

* To use Errorlevel as an environment variable, the extended syntax needs to be enabled (usually enabled by default). Also, it may not work properly if an environment variable named "ERRORLEVEL" is defined (refer to the ‘%’ page).

See also