choice - Prompt the user for input
Table of Contents
When using the command prompt or batch files, there are many situations where you want to accept user input to branch processing.
However, if you let users type freely, unintended input can cause errors.
This page introduces the basic usage and sample code of the choice command, which accepts only the keys you specify.
What Is the choice Command?
The choice command displays a message prompting the user to press one of the specified keys, and lets you branch processing according to the key that was pressed.
The basic usage of the choice command is as follows:
choice [/c <keys>] [/n] [/cs] [/t <seconds> /d <default key>] [/m <message>]
It can also be used without options, in which case it looks like this:
The pressed key is stored in the %errorlevel% variable as a number indicating its position among the choices. Details are explained below.
Options of the choice Command
The choice command has the following options:
| Option | Description |
|---|---|
/c <keys> | Specifies the keys the user can choose from. The default is YN (Y: yes, N: no). |
/n | Does not display the choices ([Y,N]?) after the prompt string. |
/cs | Makes the choice keys case-sensitive. By default, they are case-insensitive. |
/t <seconds> | Number of seconds (0 to 9999) to wait before automatically selecting the default choice if nothing is entered. Must be used with /d. |
/d <key> | Specifies the key selected on timeout. It must be one of the keys given to /c, and must be used with /t. |
/m <string> | Specifies the message to display in the prompt. |
If /t is not specified, the command waits indefinitely until the user presses a key.
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 displays Yes, and if the user inputs N, it displays 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 displays as follows:
If the user inputs Y, it displays as follows:
Get the Selection from %errorlevel%
The choice command stores in %errorlevel% not the key itself but the position (starting from 1) of the selected key in the keys given to /c.
For example, with /c YN, selecting Y gives 1 and selecting N gives 2. With /c ABC, A is 1, B is 2 and C is 3.
If the user interrupts with Ctrl + C, the value is 0.
If the options are invalid (for example, /d specifies a key that is not listed in /c), an error message is displayed and %errorlevel% is set to 255.
When there are three or more choices, combining it with goto keeps the branching readable.
@echo off
setlocal
choice /c ABC /m "Choose an action. [A:Copy, B:Move, C:Cancel]"
if %errorlevel% == 1 goto copy
if %errorlevel% == 2 goto move
goto end
:copy
echo Running copy.
goto end
:move
echo Running move.
:end
endlocal
The value of %errorlevel% is overwritten when other commands run. Check it right after choice, or save it to a variable before using it.
The older form if errorlevel 2 goto ... is true when the value is 2 or greater, so when you line up several if errorlevel statements you must test the largest number first. Writing an equality comparison such as if %errorlevel% == 2 avoids this ordering concern.
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 displays as follows:
Enable Timeout
By combining the /t and /d options, you can automatically select the default choice when there is no input for a certain time.
In the following sample code, the Y/N choices are displayed for 5 seconds, and if no selection is made, N is selected and 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 a key is pressed, the process runs as usual, but if nothing is entered for 5 seconds, No is displayed as follows:
Choosing Between choice and set /p
Besides choice, set /p can also read user input. choice accepts only the keys you define, so it suits picking from a fixed set of options, while set /p suits free-form text such as file names or paths. See the following article for set /p.
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.