The CHOICE command. (DOS 6 batch-file command) (IntroDOS) (Column)
by Tony Roberts
The CHOICE command included with DOS 6 adds an element to interactivity to batch files. With it, you can set up batch files whose courses of action depend on a user's responses to prompts.
For example, it's good to run Chkdsk or an equivalent utility each time you boot up your system. Today's larger hard disks put more programs and data at risk if undetected disk problems develop. So you include the Chkdsk command in your AUTOEXEC.BAT file, but sometimes you need to reboot the system repeatedly, and you know you don't need to check the disk each time.
The solution: Insert a CHOICE comamnd into your AUTOEXEC.BAT file. CHOICE can be configured with a default, so if you don't reply to its prompt within a specified time, it will move on without operator intervention.
The command works in combination with the DOS ERROR-LEVEL command. CHOICE asks the question; ERRORLEVEL evaluates the results.
To get an idea of how this works, enter choice at the DOS prompt and press Enter. You'll see a prompt that says [Y.N]. Now, if you press any key other than Y or N, the computer beeps and continues to wait for a response in the proper range. Once you press Y or N, the system moves on. If you don't give CHOICE any parameters, it uses Y and N as the default choices.
Now enter CHOICE /:abc Apple, Banana, or Cantaloupe? In this case, the /C: switch tells CHOICE what keys--in this case A, B, and C--are valid. You also added some text, which CHOICE uses as the prompt for the user.
Once you've set up a question for the user, you must interpret the response. This is where ERRORLEVEL comes in. When the user responds to a choice, CHOICE sets an error level in DOS. In the previous example, if the user pressed A, the error level was set to 1. If B were the choice, the error level would be set to 2, and so on. The error level numbers are set according to the order of the choices specified in the CHOICE command /C: switch.
In the first example, where no /C: switch was used, CHOICE used the default setting of /C:yn. This results in a response of Y setting the error level to 1 and N setting the error level to 2.
Immediately after issuing a CHOICE command, you test the results. You could include something like the following.
IF ERRORLEVEL 3 ECHO CANTALOUPE
IF ERRORLEVEL 2 ECHO BANANA
IF ERRORLEVEL 1 ECHO APPLE
Notice the order of the IF ERRORLEVEL commands--from highest to lowest. ERRORLEVEL doesn't test for equality; it tests to see whether the error levels is greater than or equal to the number specified. Therefore, if you reversed the order of the statements and tested first for error level 1, you'd get a true response and the system would echo APPLE even if B or C were pressed.
Now let's see how to get some work done with CHOICE. First, we'll add the /T: switch. This causes CHOICE to pause for a response, then move on after a set amount of time.
To use the /T: switch, enter the default choice and the length of the pause in seconds (this must be between 0 and 99). For example, CHOICE /C: abc /T: b, 10 Apple, Banana, or Cantaloupe? causes the computer to display the prompt as before, but if no response is entered within ten seconds, the computer uses B as the default choice.
To handle the original problem posed here--running Chkdsk--use this syntax.
CHOICE /T:y, 5 Do you want to run CHKDSK?
IF ERRORLEVEL 1 CHKDSK C:
Set up the CHOICE command properly with timers and defaults, and you can have a single AUTOEXEC.BAT file that allows plenty of flexibility. When you want to run your normal configuration, just turn on the computer and let it go. However, you could include choices to run Defrag or a virus checker, which you'd activate only when needed. Or you could include a CHOICE command that calls an alternate menu through which the kids can access their favorite software.
The CHOICE command accepts only single-digit responses. So if you're setting up a menu that includes more than nine items, label the items with letters rather than numbers.
If you want to get tricky, make the /C: switch include characters from the ANSI character set by holding down the Alt key and entering the ANSI number on the keypad. For example, if you entered Alt-205 as one of the valid choices, the prompt would show a double line similar to an equal sign. An unsuspecting user might try to enter an equal sign to select that choice, but the computer would just beep. You'd know the secret to moving on, though. You'd enter Alt-205.
Iths your system, and the CHOICE is yours--make it work for you.