Choice (command) explained

choice
Developer:Microsoft, Novell, Datalight, Jim Hall, ReactOS Contributors
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

In computing, choice is a command that allows for batch files to prompt the user to select one item from a set of single-character choices. It is available in a number of operating system command-line shells.

History

The command was first introduced as an external command (with filenames CHOICE.COM or CHOICE.EXE) with MS-DOS 6.0.[1] It is included in Novell DOS 7 and IBM PC DOS 7.0, and is also available from the command-line shell of some versions of Microsoft Windows, but not under Windows 2000 and Windows XP.[2] It was first made available for Windows in the Windows XP Resource Kit.[3] It has been reintroduced in Windows Server 2003 and is present in later versions.[4]

Starting with Windows 2000, the [[set (command)|SET]] command has similar functionality using the /P command-line argument. However this command requires an additional key stroke (hitting ENTER key), which is not required by choice.

The choice command has been ported to OS/2, Rexx and Perl.[5] It is also available in Datalight ROM-DOS,[6] FreeDOS,[7] PTS-DOS,[8] and ReactOS.[9] The FreeDOS version was developed by Jim Hall and is licensed under the GPL v2.[10]

Usage

The command returns the selected choice as an exit code which is set to the index of the key that the user selects from the list of choices. The first choice in the list returns a value of 1, the second a value of 2, and so forth.If a key is pressed that is not a valid choice, the command will sound a warning beep. If an error condition is detected, an exit code value of 255 will be returned. An exit code value of 0 will be returned if the user presses + or +.Choice displays the default choices Y and N if used without parameters.[11]

Syntax

DOS

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

Arguments:

Flags:

Microsoft Windows, ReactOS

CHOICE [/C [<Choice1><Choice2><…>]] [/N] [/CS] [/T <Timeout> /D <Choice>] [/M <"Text">]

Arguments:

Flags:

Example

The batch file below gives the user three choices.[12] The user is directed depending upon his input by evaluating the exit code using the IF ERRORLEVEL command (which tests on "greater or equal"). The selected choice is then printed to the screen using the [[echo (command)|ECHO]] command.

@ECHO off@CHOICE /C:123IF ERRORLEVEL 3 GOTO threeIF ERRORLEVEL 2 GOTO twoIF ERRORLEVEL 1 GOTO oneGOTO end

oneECHO You have pressed "1"!GOTO end

twoECHO You have pressed "2"!GOTO end

threeECHO You have pressed "3"!

end@PAUSE

Note that the example uses the DOS syntax. This example requires slight adjustments before it applies directly to Windows versions of the CHOICE command.

Note that the IF command, when checking the ERRORLEVEL, compares the number and matches if ERRORLEVEL is equal to or higher than that number. Because of this IF ERRORLEVEL comparisons should be done in decrementing order.

Note that if the user presses Control-C to escape CHOICE followed by N then the program will continue. The first "Goto end" is needed.

See also

Further reading

External links

Notes and References

  1. Web site: MS-DOS and Windows command line choice command. www.computerhope.com.
  2. Web site: Microsoft Windows XP Command-line reference A-Z. Microsoft.
  3. Web site: Choice - Accept user input - Windows CMD - SS64.com. ss64.com.
  4. Web site: Make the Choice command work for you even in Windows 7. TechRepublic. 13 October 2023 .
  5. Web site: Batch files - The CHOICE command. www.robvanderwoude.com.
  6. Web site: Datalight ROM-DOS User's Guide. www.datalight.com.
  7. Web site: ibiblio.org FreeDOS Group -- FreeDOS Base. www.ibiblio.org.
  8. Web site: PTS-DOS 2000 Pro User Manual . . Buggingen, Germany . 1999 . 2018-05-12 . live . https://web.archive.org/web/20180512094512/http://download.paragon-software.com/doc/manual_dos_eng.pdf . 2018-05-12.
  9. Web site: reactos/reactos. GitHub. 27 October 2022 .
  10. Web site: ibiblio.org FreeDOS Package -- choice (FreeDOS Base). www.ibiblio.org.
  11. Web site: Microsoft TechNet Choice article. 31 August 2016 .
  12. Web site: Batch File Help and Support. www.computerhope.com.