choice (command)
GPL v2 | |
Website | docs |
---|
In
History
The command was first introduced as an external command (with filenames CHOICE.COM or CHOICE.EXE) with
Starting with Windows 2000, the choice
.
The choice
command has been ported to
Usage
The command returns the selected choice as an
Syntax
DOS
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] ["text"]
Arguments:
/C[:]choices
Specifies allowable keys. The default is "YN"./T[:]c,nn
This defaults choice to "c" after "nn" seconds.text
Specifies the prompt string to display. (Quotes are optional).
Flags:
/N
Specifies not to display the choices and "?" at end of prompt string./S
Specifies that choice keys should be treated as case sensitive.
Microsoft Windows, ReactOS
CHOICE [/C [<Choice1><Choice2><…>]] [/N] [/CS] [/T <Timeout> /D <Choice>] [/M <"Text">]
Arguments:
/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:123
IF ERRORLEVEL 3 GOTO three
IF ERRORLEVEL 2 GOTO two
IF ERRORLEVEL 1 GOTO one
GOTO end
:one
ECHO You have pressed "1"!
GOTO end
:two
ECHO You have pressed "2"!
GOTO end
:three
ECHO You have pressed "3"!
: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.
See also
- List of DOS commands
select
in bash
References
- ISBN 0078819040.
- ^ "MS-DOS and Windows command line choice command". www.computerhope.com.
- ^ "Microsoft Windows XP Command-line reference A-Z". Microsoft.
- ^ "Choice - Accept user input - Windows CMD - SS64.com". ss64.com.
- ^ "Make the Choice command work for you even in Windows 7". TechRepublic. 13 October 2023.
- ^ "Batch files - The CHOICE command". www.robvanderwoude.com.
- ^ "Datalight ROM-DOS User's Guide" (PDF). www.datalight.com.
- ^ "ibiblio.org FreeDOS Group -- FreeDOS Base". www.ibiblio.org.
- ^
"PTS-DOS 2000 Pro User Manual" (PDF). Buggingen, Germany: Paragon Technology GmbH. 1999. Archived(PDF) from the original on 2018-05-12. Retrieved 2018-05-12.
- ^ "reactos/reactos". GitHub. 27 October 2022.
- ^ "ibiblio.org FreeDOS Package -- choice (FreeDOS Base)". www.ibiblio.org.
- ^ "Microsoft TechNet Choice article". 31 August 2016.
- ^ "Batch File Help and Support". www.computerhope.com.
Further reading
- Cooper, Jim (2001). Special Edition Using MS-DOS 6.22, Third Edition. ISBN 978-0789725738.
- Stanek, William R. (2008). Windows Command-Line Administrator's Pocket Consultant, 2nd Edition. ISBN 978-0735622623.