Table of Contents
Choice Command in Windows is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
The choice command prompts the user to select an item from the list of single character options in a batch program and then returns the index of that option. If used without parameters, the choice command will show the default options of Y and N.
For an example of how to use this command, please see the Example below.
Syntax of choice
choice [/c [<…>]] [/n] [/cs] [/t /d ] [/m <"Text">]
Parameters
Parameter Description / c Specifies the list of options created. Valid options include az, AZ, 0-9 and extended ASCII characters (128-254). The default list is "YN", displayed as [Y, N]?. / n Hide the list of options, although the choices are still active and the message content (if specified by parameter / m) is still displayed. / cs Specifies that the options are case sensitive. By default, the options are not case sensitive. / t Specifies the number of seconds to pause before using the default option specified by / d. The accepted value is from 0 to 9999. If the / t parameter is set to 0 , the selection does not pause before returning the default selection. / d Specify the default option to use after waiting for the number of seconds specified by the parameter / t. The default option must be in the list of options specified by the parameter / c. / m <"Text"> Specify a message to display before the list of options. If the parameter / m is not specified, only the prompt will be displayed. /? Show help at the command prompt.Note when using the choice command
The environment variable ERRORLEVEL is set to the index of the key that the user selects from the list of choices. The first option in the list returns a value of 1, the second option returns a value of 2, etc. If the user presses a key that is not a valid choice , choice will issue a warning beep.. If choice detects an error condition, it returns ERRORLEVEL value of 255. If the user presses CTRL + BREAK or CTRL + C , the option returns ERRORLEVEL with 0 .
Note : When you use ERRORLEVEL values in a batch program, list them in descending order.
As an example,
To display options Y, N and C, enter the following line into a batch file:
choice /c ync
The following prompt appears when the batch file runs the choice command :
[Y,N,C]?
To hide options Y, N and C, but display the text "Yes, No, or Continue", enter the following line into the batch file:
choice /c ync /n /m "Yes, No, or Continue?"
The following prompt appears when the batch file runs the choice command :
Yes, No, or Continue?
- Cmdkey command in Windows
- Cmstp command in Windows
- Color command in Windows
- Clip command in Windows
FAQ
What should you know about syntax of choice choice [/c [ ]] [/n] [/cs] [/t /d ] [/m ] Parameters Parameter Description / c Specifies the list of options created. Valid options include az, AZ, 0-9 and extended ASCII characters (128-254). The default list is "YN", displayed as [Y, N]?. / n Hide the list of options, although the choices are still active and the message content (if specified by parameter / m) is still displayed. / cs Specifies that the options are case sensitive. By default, the options are not case sensitive. / t Specifies the number of seconds to pause before using the default option specified by / d. The accepted value is from 0 to 9999. If the / t parameter is set to 0 , the selection does not pause before returning the default selection. / d Specify the default option to use after waiting for the number of seconds specified by the parameter / t. The default option must be in the list of options specified by the parameter / c. / m Specify a message to display before the list of options. If the parameter / m is not specified, only the prompt will be displayed. /? Show help at the command prompt. Note when using the choice command?
The environment variable ERRORLEVEL is set to the index of the key that the user selects from the list of choices. The first option in the list returns a value of 1, the second option returns a value of 2, etc. If the user presses a key that is not a valid.
What should you know about as an example,?
To display options Y, N and C, enter the following line into a batch file:
What is Choice Command in Windows?
The choice command prompts the user to select an item from the list of single character options in a batch program and then returns the index of that option.
Reader Comments 0
Sign in with email or Google to join the discussion.