Windows Administration Codexery

Choice (command)

Choice is a command used in batch files to prompt a user to select one item from a set of single-character choices. It is available in several operating system command-line shells and returns the selected choice as an exit code.

Quick Facts

Developer
  • Microsoft
  • Novell
  • Datalight
  • Jim Hall
  • ReactOS Contributors
Released
1993
Operating system
  • DOS
  • Windows
  • ReactOS
Platform
Cross-platform
Genre
Command
License
  • MS-DOS
  • PC DOS
  • DR-DOS
  • Windows
  • PTS-DOS: Proprietary commercial software; FreeDOS
  • ReactOS: GPL v2

Facts from the source article.

History

The command first appeared as an external file (CHOICE.COM or CHOICE.EXE) with MS-DOS 6.0. It was included in Novell DOS 7 and IBM PC DOS 7.0, and later became available in some Windows command-line shells, though not in Windows 2000 or Windows XP. The Windows XP Resource Kit made it available for Windows. It was reintroduced in Windows Server 2003 and appears in later versions. Starting with Windows 2000, the SET command with the /P argument offers similar functionality but requires an extra ENTER keystroke. The command has been ported to OS/2, Rexx, Perl, Datalight ROM-DOS, FreeDOS, PTS-DOS, and ReactOS.

Usage

The command returns the selected choice as an exit code equal to the index of the key chosen from the list. The first choice returns 1, the second returns 2, and so on. Pressing an invalid key causes a warning beep. An error condition returns exit code 255, while pressing CTRL+BREAK or CTRL+C returns exit code 0. When used without parameters, Choice displays the default choices Y and N.

Example

A batch file can present three choices and direct the user based on input using IF ERRORLEVEL, which tests on greater or equal. The example uses @ECHO OFF and @CHOICE /C:XYZ. IF ERRORLEVEL 3 branches to Zpressed, IF ERRORLEVEL 2 to Ypressed, and IF ERRORLEVEL 1 to Xpressed. Each label uses ECHO to print the pressed key. The IF ERRORLEVEL comparisons must be in decrementing order because the command matches if ERRORLEVEL is equal to or higher than the number. If the user presses Control-C and then N, the program continues; the first GOTO end is needed. The example uses DOS syntax and requires adjustments for Windows versions.

Frequently Asked Questions

Who made Choice (command)?

Choice was made by Microsoft, Novell, Datalight, Jim Hall and others.

When was Choice (command) introduced?

Choice was introduced in 1993.

More in Windows Administration

Spotted an error? Know more?

Reader corrections go straight into our review queue. Suggest an edit · How this site is sourced

Comments

Loading…
Open in the interactive codex →