| Extending CHOICE.EXE to Do Some Useful Things |
A while after the CHOICE.EXE utility was first introduced in DOS 6, I discovered that it can be used to do a lot more than prompt a user for input in a batch file. For example, it can be made to perform string manipulations, be used as a timed pause, count a loop with a numbered argument and even cause a batch file to return a selected ERRORLEVEL on exit. In addition, due to an apparent bug in CHOICE (which is still there in Win 98), it can even be used to respond to a larger compliment of keys than the documentation would suggest, e.g. function, arrow, Control-key and Alt-key combinations.
| String Parsing |
String parsing is probably the best known of the extended uses for CHOICE. As with all of the unusual functions of CHOICE, parsing strings relies on redirection or piping of input and/or output. An example that illustrates this use, PARSE.BAT, returns a parsed string in an environment variable. Note that the environment variable, RAM, in the example points to a ramdrive on my system to speed processing, but the procedure will work with or without RAM being defined.
|
:: PARSE.BAT - A routine to parse and capitalize a string
|
The procedure redirects the prompt generated by CHOICE into a temporary batch file {P}.BAT. Here, CHOICE does not wait for input from the keyboard, because the input comes from the ECHO command. As an example, if PARSE is called using "PARSE TEST=abcde", the batch file, {P}, would contain
SET[=A,B,C,D,E,;]?; after executing the third non-comment line. Calling {P} places the string into the '[' environment variable. The batch file {P}.BAT is then rewritten with a number of lines equal to the number of letters in the input string. The trailing ;]?; and all the commas are deleted in the FOR command, so that evoking {P} a second time results in a space delimited version of the original string. For example, the environment will contain the line
TEST= A B C D E for the calling sequence described above.
Parsing without changing the capitalization of the input string is accomplished by adding a /S switch to the CHOICE line to make the command case sensitive.
The approach used in PARSE does impose some limitations. It cannot handle DOS wildcard characters (?,*), the delimiters (equal sign, comma, semicolon, tab, etc.), the forward slash (/) nor the redirection characters (>, <, |). In addition, the parsed string may not be in the most usable of forms. The calling routine would still need to process it further.
A more useful method, that can handle the DOS wildcard characters, is shown below as PARSE2.BAT. This approach is nearly identical to the previous one, except that the routine calls itself (recursion) with the parsed string as a new set of command line arguments. The procedure branches to the label, Loop, on reentry to finish processing the string. Any set of commands can be installed between the LOOP and END labels to use the parsed characters.
|
:: PARSE2.BAT - A routine to parse and capitalize a string |
The second batch file takes a little longer to process the string than the first. Also, note the use of the FOR command at the end of the loop that forms a two command subroutine. For a more detailed explanation see the general notes item Minimalist Error Handlers and Mini Subroutines.
| Other Interesting Uses of CHOICE |