Home > Commands A-M > Batch Ch-Cz

CHOICE


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

Prompts the user to make a choice in a batch program. Displays a specified prompt and pauses for the user to choose from among a specified set of keys. You can use this command only in batch programs.


Syntax

CHOICE [/C[:]keys] [/N] [/S] [/T[:]c,nn] [text]

CHOICE [/C keys] [/N] [/CS] [/T c /D nn] [/M text]


Parameters
text (v6.0 Win95 NT4 ONLY)
/M text (2003)
Specifies up to 120 characters of text you want to be displayed before the prompt. Quotation marks are necessary only if you include a switch character (/) as part of the text before the prompt. If you don't specify text, CHOICE displays only a prompt.

Switches
/C[:]keys (v6.0 Win95 NT4 ONLY)
/C keys (2003)
Specifies allowable keys in the prompt. When displayed, the keys will be separated by commas, will appear in brackets ([]), and will be followed by a question mark. If you don't specify the /C switch, CHOICE uses YN as the default. The colon (:) is optional.
/N (v6.0 Win95 NT4 ONLY 2003)
Causes CHOICE not to display the prompt. The text before the prompt is still displayed, however. If you specify the /N switch, the specified keys are still valid.
/S (v6.0 Win95 NT4 ONLY)
/CS (2003)
Causes CHOICE to be case sensitive. If the /S switch is not specified, CHOICE will accept either upper or lower case for any of the keys that the user specifies.
/T[:]c,nn (v6.0 Win95 NT4 ONLY)
/T c /D nn (2003)
Causes CHOICE to pause for a specified number of seconds before defaulting to a specified key. The values for the /T switch are:
c
Specifies the character to default to after nn seconds. The character must be in the set of choices specified in the /C switch.
nn
Specifies the number of seconds to pause. Acceptable values are from 0 to 99. If 0 is specified, there will be no pause before defaulting said Microsoft but a zero won't work whether it's v6.22 or Win98. Causes CHOICE to be case sensitive. If the /S switch is not specified, CHOICE will accept either upper or lower case for any of the keys that the user specifies.

Related

For more information on the ERRORLEVEL parameter, and the IF command.
Replaced starting with WinXP with SET /P.


Notes
ERRORLEVEL parameters

The first key you assign returns a value of 1, the second a value of 2, the third a value of 3, and so on. If the user presses a key that is not among the keys you assigned, CHOICE sounds a warning beep (that is, it sends a BEL, or 07h, character to the console).

If CHOICE detects an error condition, it returns an ERRORLEVEL value of 255. If the user presses CTRL+BREAK (^BREAK) or CTRL+C (^C), CHOICE returns an ERRORLEVEL value of 0.

When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.


NT Problems

Choice.com was originally supplied on the Windows 95 install CD (and DOS v6.0 before that) and the same utility has also been released in the NT resource kits, hovever there are some problems under NT - multiple concurrent invocations of CHOICE will clobber each other. Choice.com will also burn a lot of CPU's when in a wait state.


Examples
What the user sees when you use CHOICE in a batch file

If you use this syntax in a batch file,

    CHOICE /C:YNC

the user sees when CHOICE is started:

    [Y,N,C]?

If you add text to the syntax,

    CHOICE /C:YNC Yes, No, or Continue

the user sees when CHOICE is started:

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

What the user sees if you leave out a prompt

If, you use the /N switch to leave out the prompt in a batch program,

    CHOICE /N Yes, No, or Continue?

the user sees only the text you specified when CHOICE is started:

    Yes, No, or Continue?

What the user sees if you use the T switch

If you use this syntax in a batch program,

    CHOICE /C:YNC /T:n,5

the user sees when CHOICE is started:

    [Y,N,C]?

If, after 5 seconds, the user hasn't pressed a key, CHOICE chooses N and returns an ERRORLEVEL value of 2. If the user presses a key before 5 seconds, CHOICE returns the value corresponding to the user's choice.

To have the option of defragmenting drive C when you start your computer, you could add these lines to your AUTOEXEC.BAT file:

    CHOICE Defrag drive /T:y,5
    IF ERRORLEVEL 2 GOTO SKIPDEFR
    DEFRAG C:
    :SKIPDEFR

If you press N within 5 seconds, DEFRAG 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, DEFRAG is run on drive C.


Using CHOICE in a batch program

This batch program demonstrates using the CHOICE option to select one of three programs: MS-DOS Editor, Microsoft Anti-Virus, or Microsoft Backup.

Notice that the IF ERRORLEVEL statements are listed in decreasing order. The operating system 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
     CLS
     ECHO.
     ECHO A   Microsoft Editor
     ECHO B   Microsoft Anti-Virus
     ECHO C   Microsoft Backup
     ECHO.
     CHOICE /C:abc Choose an option
     IF ERRORLEVEL 3 GOTO MSBACKUP
     IF ERRORLEVEL 2 GOTO MSAV
     IF ERRORLEVEL 1 GOTO EDIT
    :EDIT
     EDIT
     GOTO END
    :MSAV
     MSAV
     GOTO END
    :MSBACKUP
     MSBACKUP
     GOTO END
    :END

Setting a specific ERRORLEVEL

CHOICE can be used to set a specific ERRORLEVEL for example to set the ERRORLEVEL to 6:

     ECHO6|CHOICE /C:123456 /N >NUL

Errorlevels
Availability
External
DOS
v6.0 v6.10 v6.2 v6.21 v6.22 v7.00 v7.0R1 v7.10 v8.00
Windows
Win95 Win98 WinME
Windows NT
NT2003
External NT Resource Kit
DOS
none
Windows
none
Windows NT
NT4