About the CHOICE command
In a batch program, it prompts the user to make a choice. It displays a specified
prompt and pauses, waiting for the user to choose from a specified set of keys. This
command can only be used in batch programs.
For a detailed explanation of the ERRORLEVEL parameter, see the and commands.
Syntax
CHOICE keys] c,nn]
Parameter
text
Specifies the text to display before the prompt. Double quotation marks are needed
only when the text before the prompt includes the switch character
(/) as part of the text. If no text is specified, CHOICE only dis-
plays a prompt.
Switches
/Ckeys
Specifies the keys accepted in the prompt. When displayed, these keys appear inside
, separated by commas, followed by a question mark. If the /C switch is not
specified, CHOICE uses the default value YN. The colon (

is optional.
/N
Makes CHOICE not display the prompt, but the text before the prompt is still shown.
If the /N switch is specified, the specified keys are still valid.
/S
Makes CHOICE case-sensitive. If /S is not specified, CHOICE does not distinguish
between uppercase and lowercase for keys entered by the user.
/Tc,nn
Before using a specified key as the default choice, CHOICE waits for a specified
number of seconds. The values in the /T switch are as follows:
c
Specifies the character to use as the default after waiting nn seconds. This
character must appear in the set of keys after the /C switch.
nn
Specifies the pause time in seconds. Acceptable values are from 0 to 99. If
set to 0, there is no pause before using the default.
CHOICE—Examples
To let the user see prompt information in a batch file, you can use the CHOICE
command in the batch file.
For example, if the following syntax is used in a batch file:
choice /c:ync
then when CHOICE runs, the user will see:
?
Adding text to the above example:
choice /c:ync Yes, No, or Continue
choice /c:ync Yes, No, or Continue
When CHOICE starts, the user sees:
Yes, No, or Continue ?
What the user sees after removing the prompt
As in the following example, use the /N switch in a batch program to remove the prompt:
choice /n Yes, No, or Continue?
When CHOICE executes, the user sees only the specified text:
Yes, No, or Continue?
What the user sees after using the T switch
If the following statement is used in a batch program:
choice /c:ync /t:n,5
When CHOICE executes, the user will see:
?
After 5 seconds, if the user has not pressed any key, CHOICE selects N and returns
an ERRORLEVEL value of 2. If a key is pressed within 5 seconds, CHOICE returns the value
corresponding to the user's choice.
When starting the computer, if you want the user to choose whether to defragment
drive C, add the following lines to the AUTOEXEC.BAT file:
choice Defrag drive /ty,5
if errorlevel 2 goto SkipDefrag
defrag c:
:SkipDefrag
If N is pressed within 5 seconds, DEFRAG will not run, and CHOICE returns an
ERRORLEVEL value of 2. If N is not pressed within 5 seconds, or Y is chosen, DEFRAG will
run on drive C.
Using CHOICE in a batch program
The following batch program demonstrates how to use CHOICE options to let the user
choose one of three programs to run:
MS-DOS Editor, Microsoft Anti-virus, or Microsoft Backup.
Note that the IF ERRORLEVEL statements here are listed in descending order. If the
value of the ERRORLEVEL parameter returned by CHOICE is greater than or equal to the
parameter specified by the IF command, MS-DOS treats the IF statement as true.
@echo off
cls
echo.
echo A Microsoft Editor
echo B Microsoft Anti-Virus
echo C Microsoft Backup
echo.
choice /c:abc Choose an option
if errorlevel 3 goto MSBackup
if errorlevel 2 goto Msav
if errorlevel 1 goto Edit
:Edit
edit
goto End
:Msav
msav
goto End
:Msbackup
msbackup
goto End
:End