Choice

Index

CHOICE prompts the user to select one of a specified set of keys in a batch program.

Syntax:

CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
/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.
text Text displayed before the prompt. Typically, text is used to expand on the subsequent prompt.

Notes:

The CHOICE command works by assigning an errorlevel value to each entry under choices (see below).

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]?

    CHOICE /C:ync

    [Y,N,C]?

    CHOICE /C:ync Yes, No, Continue

    Yes, No, Continue [Y,N,C]?

    CHOICE /n Yes, No, or Continue?

    Yes, No, Continue?

    CHOICE /C:ync /T:N,5

    [Y,N.C]?

    for 5 seconds; if no key has been pressed by then, N 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

    If you press N within 5 seconds, SCANDISK will not run and CHOICE returns an ERRORLEVEL value of 2. If you do not press N 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 /C:abc Choose an option
    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 NameVersionSizeDateDefault Location
Choice.com Win95: Upgrade5 17511/07/95c:\windows\command
Win95: OSR2.55 17524/08/96c:\windows\command

If you should have any comments or suggestions,
please contact: Bob Watson
.
This page last revised:
December 9, 1999.