Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
CHOICE--Examples
◄Syntax► ◄Note►
────────────────────────────────────────────────────────────────────────────
CHOICE──Examples
What the user sees when you use CHOICE in a batch file
If you use the following syntax in a batch file,
choice /c:ync
the user sees the following when CHOICE is started:
[Y,N,C]?
If you add text to the syntax,
choice /c:ync Yes, No, or Continue
the user sees the following when CHOICE is started:
Yes, No, or Continue [Y,N,C]?
What the user sees if you leave out a prompt
If, as in the following example, you use the /N switch to leave out the
prompt in a batch program,
choice /n Yes, No, or Continue?
the user sees only the text you specified when CHOICE is started:
Yes, No, or Continue?
What the user sees if you use the T switch
If you use the following syntax in a batch program,
choice /c:ync /t:n,5
the user sees the following when CHOICE is started:
[Y,N,C]?
If, after 5 seconds, the user hasn't pressed a key, CHOICE chooses N and
returns an ERRORLEVEL value of 2. If the user presses a key before 5
seconds, CHOICE returns the value corresponding to the user's choice.
To have the option of defragmenting drive C when you start your computer,
you could add the following lines to your AUTOEXEC.BAT file:
choice Defrag drive /ty,5
if errorlevel 2 goto SkipDefrag
defrag c:
:SkipDefrag
If you press N within 5 seconds, DEFRAG will not run and CHOICE returns an
ERRORLEVEL value of 2. If you do not press N within 5 seconds, or if you
choose Y, DEFRAG is run on drive C.
Using CHOICE in a batch program
The following batch program demonstrates using the CHOICE option to select
one of three programs: MS-DOS Editor, Microsoft Anti-Virus, or Microsoft
Backup.
Notice that the IF ERRORLEVEL statements are listed in decreasing order.
MS-DOS will consider the IF statement true if the ERRORLEVEL parameter
returned by CHOICE is greater than or equal to the parameter specified in
the IF command.
@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
♦