Choice (command) - Example

Example

The batch file below gives the user three choices. 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 off @CHOICE /C:123 IF ERRORLEVEL 3 GOTO three IF ERRORLEVEL 2 GOTO two IF ERRORLEVEL 1 GOTO one GOTO end :one ECHO You have pressed "1"! GOTO end :two ECHO You have pressed "2"! GOTO end :three ECHO 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.

Read more about this topic:  Choice (command)

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)