Choice

Index

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.

Syntax:

CHOICE [text] [/C[:]choices] [/N] [/S] [/T[:]c,nn]
/C[:]choicesThe 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
/NStops the prompt from being displayed, although text is still shown and the choices keys are still valid.
/SForces 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.
textText displayed before the prompt. Typically, text is used to expand on the subsequent prompt.

Notes:

  1. 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.

  2. 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.

  3. 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!

  4. 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.

Tricks

  1. 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.

  2. 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!

Exit Codes:

0CHOICE was terminated by Ctrl-C before a choice was entered.
1The key corresponding to the first choice was pressed.
nThe key corresponding to the nth choice was pressed.
255An error occurred.

Examples:

  1. Some examples of CHOICE command formats and the corresponding display:

    CHOICE

    [Y,N]?

    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.


    CHOICE /C:ync

    [Y,N,Q]?

    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

    [Y,Q]?

    The prompt is displayed for 5 seconds; if no key has been pressed by then, Q is chosen returning an ErrorLevel value of 2.


  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
    Scandisk c:
    :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.

  3. 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 off
    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
    :Dbug
    Debug
    GOTO End
    :Scan
    Scandisk
    GOTO End
    :Msbackup
    msbackup
    GOTO End
    :End

File Details

File NameDefault LocationDos Ver.Win Ver.SizeDateSource
Choice.comc:\windows\command 7.0Win95 5 175111/07/95win95_08.cab
7.1Win95 (OSR2.x) 5 175124/08/96win95_13.cab
Win98 5 239211/05/98win95_28.cab
Win98 SE 5 239223/04/99win98_25.cab

Superscripts denote which same size files, if any, are identical (using FC).


If you should have any comments or suggestions,
please contact: Bob Watson
.
This page last revised:
November 1, 2000.