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.

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.

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

Note:

In this example, if the user does not press the key within five seconds, the choice command will select N by default and return an error value of 2. Otherwise, the choice command returns the value corresponding to the user's choice. .

See more:

  1. Cmdkey command in Windows
  2. Cmstp command in Windows
  3. Color command in Windows
  4. Clip command in Windows
4 ★ | 5 Vote