Starting with Windows 2000, the SET command has similar functionality using the /Pcommand-line argument. However this command requires an additional key stroke (hitting ENTER key), which is not required by choice.
The command returns the selected choice as an exit code which is set to the index of the key that the user selects from the list of choices. The first choice in the list returns a value of 1, the second a value of 2, and so forth.
If a key is pressed that is not a valid choice, the command will sound a warning beep. If an error condition is detected, an exit code value of 255 will be returned. An exit code value of 0 will be returned if the user presses CTRL+BREAK or CTRL+C.
Choice displays the default choices Y and N if used without parameters.[12]
/C[:]choices Specifies allowable keys. The default is "YN". (Microsoft Windows restricts valid choice keys to a-z, A-Z, 0-9 and ASCII values of 128 to 254)
/T[:]nn This defaults choice to /D after "nn" seconds. Must be specified with default /D.
/D[:]c This defaults choice to 'c'.
/M text Specifies the prompt string to display.
Flags:
/N Specifies not to display the choices and "?" at end of prompt string.
/CS Specifies that choice keys should be treated as case sensitive.
Example
The batch file below gives the user three choices.[13]
The user is directed depending upon his input by evaluating the exit code using the IF ERRORLEVEL command (which tests on "greater or equal"). The selected choice is then printed to the screen using the ECHO command.
@ECHO OFF
@CHOICE /C:XYZ
IFERRORLEVEL3GOTOZpressedIFERRORLEVEL2GOTOYpressedIFERRORLEVEL1GOTOXpressedGOTOend:XpressedECHO You have pressed "X"!
GOTOend:YpressedECHO You have pressed "Y"!
GOTOend:ZpressedECHO You have pressed "Z"!
:end@PAUSE
Note that the example uses the DOS syntax. This example requires slight adjustments before it applies directly to Windows versions of the CHOICE command.
Note that the IF command, when checking the ERRORLEVEL, compares the number and matches if ERRORLEVEL is equal to or higher than that number. Because of this IF ERRORLEVEL comparisons should be done in decrementing order.
Note that if the user presses Control-C to escape CHOICE followed by N then the program will continue. The first "Goto end" is needed.