Sideway
output.to from Sideway
Draft for Information Only

Content

MS DOS Command: CHOICE
  Description
  Syntax
  Parameters
  Switches
  Remarks
   ERRORLEVEL parameters
  Examples

MS DOS Command: CHOICE

Reference from Microsoft MS-DOS cmd help

Description

Prompts the user to make a choice in a batch program. Displays a specified prompt and pauses for the user to choose from among a specified set of keys. You can use this command only in batch programs.

For more information on the ERRORLEVEL parameter, see CHOICE--Note and the IF command.

Syntax

CHOICE [/C[:]keys] [/N] [/S] [/T[:]c,nn] [text]

Parameters

text Specifies text you want to be displayed before the prompt. Quotation marks are necessary only if you include a switch character (/) as part of the text before the prompt. If you don't specify text, CHOICE displays only a prompt.

Switches

/C[:]keys Specifies allowable keys in the prompt. When displayed, the keys will be separated by commas, will appear in brackets ([]), and will be followed by a question mark. If you don't specify the /C switch, CHOICE uses YN as the default. The colon (:) is optional.
/N Causes CHOICE not to display the prompt. The text before the prompt is still displayed, however. If you specify the /N switch, the specified keys are still valid.
/S Causes CHOICE to be case sensitive. If the /S switch is not specified, CHOICE will accept either upper or lower case for any of the keys that the user specifies.
/T[:]c,nn Causes CHOICE to pause for a specified number of seconds before defaulting to a specified key. The values for the /T switch are as follows:
c Specifies the character to default to after nn seconds. The character must be in the set of choices specified in the /C switch.
nn Specifies the number of seconds to pause. Acceptable values are from 0 to 99. If 0 is specified, there will be no pause before defaulting.

Remarks

ERRORLEVEL parameters

The first key you assign returns a value of 1, the second a value of 2, the third a value of 3, and so on. If the user presses a key that is not among the keys you assigned, CHOICE sounds a warning beep (that is, it sends a BEL, or 07h, character to the console).

If CHOICE detects an error condition, it returns an ERRORLEVEL value of 255. If the user presses CTRL+BREAK or CTRL+C, CHOICE returns an ERRORLEVEL value of 0.

When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.

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


©sideway

ID: 110700141 Last Updated: 7/22/2011 Revision: 0


Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Travel 18

Reference 79

Computer

Hardware 254

Software

Application 213

Digitization 37

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 431

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Set 1

Logic 1

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2025 Sideway . All rights reserved Disclaimers last modified on 06 September 2019