Using MS-DOS CHOICE Command in Batch Files

The CHOICE command in MS-DOS 6.22 enables interactive batch scripts by pausing execution and waiting for the user to press a single key from a predefined set of options. Rather than running non-interactively from start to finish, a script can use CHOICE to present menus, confirm file operations, or branch logic based on real-time decisions. The command captures the keystroke, validates it against allowed inputs, and reports the selection back to the environment using exit codes known as ERRORLEVEL.

Basic Syntax and Parameters

The standard syntax for the CHOICE command is:

CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
  • text: The prompt string displayed to the user on the screen.
  • /C[:]choices: Specifies the allowed keys. If omitted, MS-DOS defaults to YN (Yes/No).
  • /N: Prevents CHOICE from displaying the prompt brackets containing the valid keys at the end of the text prompt.
  • /S: Enables case sensitivity. By default, CHOICE ignores case.
  • /T[:]c,nn: Sets a default key choice (c) after a specified number of seconds (nn, from 0 to 99) if the user does not press a key.

How Input Is Captured via ERRORLEVEL

The CHOICE command does not store text in environment variables. Instead, it maps the chosen key to an integer value stored in ERRORLEVEL based on its position in the /C parameter list:

  • Pressing the first key returns an ERRORLEVEL of 1.
  • Pressing the second key returns an ERRORLEVEL of 2.
  • Pressing Ctrl+C or Ctrl+Break returns an ERRORLEVEL of 0.
  • A detection error returns an ERRORLEVEL of 255.

Any key pressed by the user that is not in the allowed list triggers an alert beep, and MS-DOS continues waiting until a valid key is pressed.

Evaluating Input in Batch Logic

In MS-DOS, the IF ERRORLEVEL n conditional evaluates to true if the return code is greater than or equal to n. Because of this behavior, batch scripts must always check ERRORLEVEL values in descending order (highest number to lowest number). If checked in ascending order, an ERRORLEVEL of 3 would trigger the check for 1, producing incorrect logic branching.

Example Implementation

Below is a practical implementation creating a three-option interactive menu:

@ECHO OFF
ECHO Select an option:
ECHO 1. Run Scandisk
ECHO 2. Run Defrag
ECHO 3. Exit

CHOICE /C:123 /N "Enter your choice [1, 2, or 3]: "

IF ERRORLEVEL 3 GOTO END
IF ERRORLEVEL 2 GOTO DEFRAG
IF ERRORLEVEL 1 GOTO SCANDISK

:SCANDISK
SCANDISK C:
GOTO END

:DEFRAG
DEFRAG C:
GOTO END

:END
ECHO Operation complete.

In this script, pressing 3 routes directly to :END. Pressing 2 bypasses the check for 3 and routes to :DEFRAG. Pressing 1 executes the :SCANDISK routine, cleanly separating application flow using native MS-DOS 6.22 capabilities.