Computer and Software Support


Choice

This page is from Microsoft

Updated: January 21, 2005

Prompts the user to make a choice in a batch program by displaying a prompt and pausing for the user to choose from a set of user-option keys.
 
 
Syntax
choice [/c [Choices]] [/n] [/cs] [/tTimeout/dChoice] [/mText]
Parameters
/cChoices
Specifies the user-option keys. When displayed, the option keys are separated by commas, appear in brackets ([]), and are followed by a question mark. If you do not specify /cChoices, choice displays [Y, N] by default.
/n
Hides the user-option keys. If you use this command-line option, the message text before the prompt appears and options are still enabled.
/cs
Specifies that the user-option keys must be case-sensitive. By default, user-option keys are not case-sensitive.
/tTimeout/dChoice
Specifies that choice pause for a specified number of seconds before defaulting to a specified key. The values for the /t command-line option are described in the following table.ValueDescriptionTimeoutThe number of seconds to pause. Acceptable values are from 0 to 9999. If you use 0, choice does not pause before defaulting to the specified key./d ChoiceSpecifies the default choice after Timeout seconds. The character that you use must be in the set of choices specified by /cChoices. If you use this command-line option, you must also specify /tTimeout.
/mText
Specifies the text that you want to display before the prompt. If you include a command-line option character (that is, /) as part of the text before the prompt, you must wrap the text in quotation marks. If you do not specify any text, choice displays a prompt only.
/?
Displays help at the command prompt.
Remarks
  • The ERRORVALUE environment variable is set to the index of the key that the user selects from the list of choices. 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.exe sounds a warning beep (that is, it sends a BEL or 07h character to the console). If Choice.exe detects an error condition, it returns an error value of 255. If the user presses CTRL+BREAK or CTRL+C, Choice.exe returns an error value of 0. When you use error value parameters in a batch program, list them in decreasing order.

 
Examples
Type the following syntax in a batch file:

choice /c ync

The following appears when you run Choice.exe:

[Y,N,C]?

Type the following syntax in a batch file:

choice /c ync /n /m Yes, No or Continue?

The following appears when you run Choice.exe:

Yes, No, or Continue?

Add text to the syntax as follows:

choice /c ync /mYes, No, or Continue

The following appears when you run Choice.exe:

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

The /t command-line option sets a time limit within which the user must respond and specifies the value to display if the user does not respond within that time limit. To set a time limit of five seconds and specify N as the default value, type the following line in a batch program:

choice /c ync /t 5 /d n

When the batch file starts Choice.exe, the following message appears:

[Y,N,C]?

If the user does not press a key within five seconds, Choice.exe selects N and returns an error value of 2. Otherwise, Choice returns the value corresponding to the user's choice.

Formatting legend

Format Meaning
Italic Information that the user must supply
Bold Elements that the user must type exactly as shown
Ellipsis (...) Parameter that can be repeated several times in a command line
Between brackets ([]) Optional items
Between braces ({}); choices separated by pipe (|). Example: {even|odd} Set of choices from which the user must choose only one
Courier font Code or program output