CHOICE prompts the user to select one of a specified set of keys in a batch program. It is the only Dos command that allows for controlled user input to a batch file.
CHOICE [text] [/C[:]choices] [/N] [/S] [/T[:]c,nn]
|/C[:]choices||The prompt - comprising a series of single alphanumeric characters corresponding to valid choices. The prompt is displayed as the characters, separated by commas, enclosed in brackets, and followed by a question mark. Default is YN|
|/N||Stops the prompt from being displayed, although text is still shown and the choices keys are still valid.|
|/S||Forces the choice keys to be case sensitive. By default they are not.|
|/T[:]c,nn ||Defaults choice to c after nn seconds. |
c must exist; nn must be in the range 0 to 99.
|text||Text displayed before the prompt. Typically, text is used to expand on the subsequent prompt.|
- The CHOICE command works by assigning an errorlevel value (exit code) to each entry under choices (see "Exit Codes" below) and is almost always immediately followed by one or more IF (NOT) ERRORLEVEL statements.
- Although text can be up to 120 characters in length, it doesn't wrap well. For menus or, indeed, anything more than the simplest of prompts, it is usual to preceed the CHOICE command with a series of ECHO statements or insert a text file using TYPE. Either of these methods can be used to lay out a formatted screen and the CHOICE command would probably not need to display anything but the flashing cursor.
- If the /S switch is used to render the choices case specific, the number of possible choices is limited only by the number of single alpha-numeric characters available in the extended ASCII character set (excluding <, >, and |) - over 200 and enough to make for a very cumbersome prompt!
- Any keystroke apart from those specified in choices (and Ctrl-C which gives the option of terminating the batch file) is ignored. However, if the /T switch is used in order to default to a particular choice after a specified time, pressing an invalid key will disable the timer. This is, apparently, a long standing bug in the CHOICE command.
- The CHOICE command can be used to introduce a timed delay in a batch file. For this purpose, there are no prompts, one choice and, because of the abovementioned bug, there should be no possibility of user intervention. A typical syntax for a timed delay might be:
TYPE NUL|CHOICE /C:Y /T:Y,nn > NUL
where the TYPE NUL part pipes nothing to the CHOICE command that has one choice ("Y") to which it defaults after nn seconds (1-99), and the output is redirected to NUL.
- A very basic password protection for a batchfile could be introduced using CHOICE and not displaying the prompt. For example:
CHOICE Enter password to continue: /C:>> /N
would halt the batch file until Alt-175 (>>) was pressed. Not exactly unbreakable!
|0||CHOICE was terminated by Ctrl-C before a choice was entered.|
|1||The key corresponding to the first choice was pressed.|
|n||The key corresponding to the nth choice was pressed.|
|255||An error occurred.|
- Some examples of CHOICE command formats and the corresponding display:
Completely unadorned, Choice will stop processing a batch file until the user presses either Y or N (upper or lower case) at which point the batch file will resume. Pressing "Y" or "y" will return an errorlevel of 1, "N" or "n" will return an errorlevel of 2 and any other key will be ignored. Usually the next line(s) of the batch file will include a test to determine the current errorlevel value along with instructions on what should be done if the errorlevel is greater than or equal to some particular value.
A third option has been added and pressing "Q" or "q" will return an errorlevel of 3.
CHOICE Run Scandisk: Yes, Skip, Quit /C:ysq
|Run Scandisk: Yes, Skip, Quit [Y,S,Q]?|
Including some text ("Run Scandisk: Yes, Skip, Quit") can clarify the prompt.
CHOICE Press "F" to format the disk or "Q" to quit: /C:fq /n
|Press "F" to format the disk or "Q" to quit: |
The prompt [F,Q]? that would normally follow the text (Press "F" to format the disk or "Q" to quit:) has here been suppressed.
CHOICE /C:yq /T:q,5
The prompt is displayed for 5 seconds; if no key has been pressed by then, Q is chosen returning an ErrorLevel value of 2.
- To have the option of running SCANDISK on drive C when the computer is turned on, the following lines could be added to "autoexec.bat":
CHOICE Run Scandisk /ty,5
IF errorlevel 2 GOTO SkipScan
Bearing in mind that as no choices were specified, the defaults of Y and N were used. If N is pressed within 5 seconds, SCANDISK will not run and CHOICE returns an ERRORLEVEL value of 2. If N is not pressed within 5 seconds, or if you choose Y, SCANDISK runs on drive C.
- The following batch program uses the CHOICE option to select one of three Dos utilities: Scandisk, Microsoft Backup, or Debug. Notice that the IF ERRORLEVEL statements are listed in decreasing order. MS-Dos will consider the IF statement true if the ERRORLEVEL parameter returned by CHOICE is greater than or equal to the parameter specified in the IF command.
ECHO A Scandisk
ECHO B Microsoft Backup
ECHO C Debug
CHOICE Choose an option /C:abc
IF errorlevel 3 GOTO Dbug
IF errorlevel 2 GOTO MsBack
IF errorlevel 1 GOTO Scan
|File Name||Default Location||Dos Ver.||Win Ver.||Size||Date||Source|
Superscripts denote which same size files, if any, are identical (using FC).