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. 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

May be interested

  • Rem command in WindowsRem command in Windows
    the rem command helps write comments (comments) in a batch file or config.sys.
  • Instructions for using Command PromptInstructions for using Command Prompt
    deep in windows is a command-line world that is obscure. in this article, we will show you how to solve errors and make your computer more secure.
  • Rd command in WindowsRd command in Windows
    the rd command helps delete a directory
  • Cmd command in WindowsCmd command in Windows
    the cmd command starts a new version of the command interpreter, cmd.exe. if used without parameters, cmd will display copyright information and the version of the operating system.
  • How to use the command history function in Command PromptHow to use the command history function in Command Prompt
    command prompt is an extremely familiar command for anyone using windows operating system. besides, a lot of current software also supports the command line to perform actions on the command prompt window, instead of on the screen.
  • The sfc command in WindowsThe sfc command in Windows
    (applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)
  • Reg command copy in WindowsReg command copy in Windows
    to copy an entry to the location specified on the local pc or the remote computer we will use the reg copy command. to learn more about how to use the reg copy command, you can track the syntax, explain the command parameters and the example tipsmake.com has compiled below.
  • Fc command in WindowsFc command in Windows
    the fc command compares two files or a collection of files and displays the differences between them.
  • The echo command in WindowsThe echo command in Windows
    the echo command displays the message or turns on / off the command repeat feature. if used without parameters, the echo command will display the current echo setting.
  • Xcopy command in WindowsXcopy command in Windows
    the xcopy command is a command prompt command used to copy one or more files or folders from one location to another. with many options and the ability to copy entire directories, the xcopy command is similar to, but much more powerful, than the copy command.