choice - Prompt the user for input

Maintained on

There are many situations where you want to prompt the user for input to branch the processing when using the command prompt or batch files.

However, if you allow free input, there is a possibility of errors occurring due to unintended input.

This page introduces the basic usage and specific sample code of the choice command, which accepts input from the user for specified keys.

What is the choice command?

The choice command displays a message that prompts the user to enter specified keys and allows you to branch the processing according to the entered key.

The basic usage of the choice command is as follows.

choice [/c [<choice keys>]] [/n] [/cs] [/t <timeout seconds> /d <default choice>] [/m <message>]

You can also use the choice command without specifying any options, in which case it will look like this:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>choice
[Y,N]?

Options for the choice command

The choice command has the following options.

OptionDescription
/C:[keys]Specifies the keys that the user can select. By default, the user can select Y (yes) or N (no).
/NDoes not display the choices after the prompt string.
/SMakes the choices case-sensitive. By default, the choices are not case-sensitive.
/T:[seconds]Specifies the number of seconds before the user must press a key. If you do not specify this option, the user can wait indefinitely.
/D:[key]Specifies the default key to use if the user does not press a key within the specified number of seconds. Must be used with /T.
/M:[message]Specifies the message to display in the prompt.

Specific usage of the choice command

Branch processing according to user input

The most basic usage of the choice command is to branch processing according to user input.

In the following sample code, if the user inputs Y, it will display Yes, and if the user inputs N, it will display No.

@echo off
setlocal

choice /c YN /m "Yes or No?"

if %errorlevel% == 1 (
  echo Yes
) else (
  echo No
)

endlocal

When you run the above batch file, it will display as follows:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>
Yes or No?[Y,N]?

If the user inputs Y, it will display as follows:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>
Yes or No?[Y,N]? Y
Yes
C:\users\user>

Display a message and choices

You can display a message and choices by combining the /c option and the /m option.

In the following sample code, the user is prompted to choose their favorite fruit, and the selected fruit is displayed.

@echo off
setlocal

choice /c AB /m "Please choose your favorite fruit. [A: Apple, B: Banana]"

if %errorlevel% == 1 (
  echo Apple
) else (
  echo Banana
)

endlocal

When you run the above batch file, it will display as follows:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>
Please choose your favorite fruit. [A: Apple, B: Banana]? [A,B]? A
Apple
C:\users\user>

Enable timeout

By specifying the /t option, you can specify the number of seconds until the user presses a key.

In the following sample code, the Y/N choices are displayed for 5 seconds, and if no selection is made, No is displayed.

@echo off
setlocal

choice /c YN /t 5 /d N /m "Yes or No?"

if %errorlevel% == 1 (
  echo Yes
) else (
  echo No
)

endlocal

When you run the above batch file, if an option is selected, the process will be executed as usual, but if nothing is entered for 5 seconds, No will be displayed as follows:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>
Yes or No?[Y,N]? N
No
C:\users\user>

Confirmation Test

Finally, let’s try a confirmation test.

The test is completed on the client side, so no data is sent to the server.

Quiz1

What is the choice command in Command Prompt used for?

回答がサーバーに送信されることはありません
Quiz2

How do you use the input received from the user with the choice command?

回答がサーバーに送信されることはありません
Quiz3

What is the default prompt message for the choice command?

回答がサーバーに送信されることはありません
#Command Prompt #Batch Files #Command Line #Command #User Input