I often find myself writing batch files for both myself and
others, wishing that I had some sort of input capability,
just enough to be able to branch depending on a choice made
by the user while the batch file was running.
Some utilities that do this already exist, a few written for
PC magazine, some just public domain, and even one in the
latest version of the Norton utilities. All of these
utilities communicate to the batch file through the DOS
ERRORLEVEL, which I will review below.
But first, an explanation. At the time I wrote this program
I couldn't find one that behaved the way I wanted. The usual
approach is just to accept a single keystroke and set the
ERRORLEVEL variable to it's ascii value. This requires some
fiddling around on the part of the batch file to accommodate
different case letters and invalid responses, not to mention
having to keep track of the ascii value of various
characters. Also, these utilities don't allow you to specify
I wanted an input utility that would:
Let me specify a prompt
Let me specify what characters would be valid input, and
not allow any other characters.
Would not require a carriage return to be pressed after
typing the letter.
Would return an errorlevel that made sense based on the
valid input that I specified.
I think I've succeeded. Here is the help that the program
displays when you just type BATQUEST...
Format is: BATQUEST [ ]
where: is a prompt string enclosed in double quote marks, and
is required. However, you may make it null or blank.
is an optional character string whose characters are the
valid responses to the prompt, in increasing ERRORLEVEL
order. Embedded blanks are not allowed. Lowercase letters
are translated to uppercase. must be less than
40 characters long, and if any character is duplicated,
only the last occurance will be found. If is not
present, then any character is a valid answer to the prompt.
Example. BATQUEST "Enter your choice ==> " 123ABCQ
will display the message: Enter your choice ==>
which can only be answered with one of the characters: 123ABCQ
which will return an ERRORLEVEL of 4 if answered with: A
which will return an ERRORLEVEL of 7 if answered with: Q
written for the Public Domain by David Michmerhuizen, Oakland CA, 1986
Source code in Turbo C is included. You may compile it with
the small model into BATQUEST.EXE, or (as I did), compile it
with the Tiny model, type EXE2BIN BATQUEST.EXE BATQUEST.COM,
and delete batquest.exe.
BATQUEST.COM is also included for you non-hacker types, as
well as two demo batch files.
The DOS manual (you do have one, don't you?), covers the
ERRORLEVEL variable fairly well, but anyway, here's a quick
DOS has an internal variable named ERRORLEVEL that a
program can set upon completion to signal normal or
abnormal conditions. Strangely enough, there is only one
DOS command that uses this variable, and I've forgotten
what it is. This lack of recognition by the writers of
DOS does not make it any less useful however. You test
the ERRORLEVEL setting in a batch file like this...
IF ERRORLEVEL n command
where n is an errorlevel number, and command is some
command to execute. For example...
IF ERRORLEVEL 2 GOTO OOPS
this statement will be TRUE (and GOTO OOPS will be
executed), for any ERRORLEVEL that is 2 *or more*. In
other words, a statement such as this is actually a way of
saying IF ERRORLEVEL >= 2 GOTO OOOPS
Written for the Public Domain (long may it wave) by
c/o Centerville BBS, (415) 793-3037