Dec 122017
 
Useful batch utility that allows a batch file to ask a multiple choice questions, and evaluate the answer with errorlevel.
File QUERY3.ZIP from The Programmer’s Corner in
Category Batch Files
Useful batch utility that allows a batch file to ask a multiple choice questions, and evaluate the answer with errorlevel.
File Name File Size Zip Size Zip Type
QUERY.ASM 4136 1200 deflated
QUERY.COM 104 104 stored
QUERY.DOC 6580 2629 deflated

Download File QUERY3.ZIP Here

Contents of the QUERY.DOC file


--- QUERY.COM ---
Version 3.0

This program is designed to allow batch files to ask a multiple choice
question, and react according to the answer. It is run by placing

QUERY @[possible responses]

in the batch file. For example,

QUERY Press letter desired: @ABCD

would cause the computer to print "Press letter desired: ", and then
wait for the user to hit a key. After a key from the list is hit, the
program ends.

The response is passed on to the DOS batch file as an ErrorLevel. The
first possible response generates an ErrorLevel of 1, the second
generates an ErrorLevel of 2, etc. In the above example, a response of
"A" would cause the ErrorLevel to be set to 1, "B" would result in an
ErrorLevel of 2, and "C" would produce an ErrorLevel of 3. QUERY
automatically sets alpha characters to upper case; a response of "a" is
the same as "A". Non-alpha charates are left in normal state, such that
numbers or symbols may be used if desired.

If a key is hit that is not one of the valid responses, the program
waits for another key to be hit, until a match is found. When a match
is found, the key will be printed in upper case. Any errors occurring
cause the ErrorLevel to be set to 255.

There can be as many as 127 possible responses, and they don't have to
be letters or numbers. They can be almost any printable character.

Here is an example of a batch file using QUERY:

1 @ECHO OFF
2 ECHO [0m
3 :start
4 CLS
5 TYPE c:\bat\menu.txt
6 QUERY [17;28f[30;46mPress letter desired: @ABCD
7 ECHO [19;1f[37;44m
8 IF ERRORLEVEL 255 GOTO ERROR
9 IF ERRORLEVEL 4 GOTO QUIT
10 IF ERRORLEVEL 3 GOTO OPERATION3
11 IF ERRORLEVEL 2 GOTO OPERATION2
12 IF ERRORLEVEL 1 GOTO OPERATION1
13 GOTO START
14 :OPERATION1
15 REM this is where the first choice program is inserted
16 GOTO START
17 :OPERATION2
18 REM this is where the second choice program is inserted
19 GOTO START
20 :OPERATION3
21 REM this is where the third choice program is inserted
22 GOTO START
23 :ERROR
22 ECHO An error has occurred!
23 :QUIT

In this example, an error (ErrorLevel 255) would cause the program to
abort with an error message. Invalid responses do no harm; the program
will wait for a correct one. [Ctrl]-[Break] will cause a prompt to
operating asking whether or not an abort is desired. Using the technique
on Line-5 is quicker than using Echo, when there are several menu options,
and when a lot of fancy line and box characters are desired. ANSI escape
sequences are used to pretty-up the screen and possition the cursor within
the menu painted by the TYPE command. ANSI.SYS or a replacement must be
loaded for this technique to work.

Note that the ErrorLevel is tested for in reverse order. This is
because DOS considers the statement "if errorlevel==x ..." to be True if
the current ErrorLevel is set to the number x OR HIGHER. Thus, if the
batch file had in line 8, "IF ERRORLEVEL==1 GOTO OPERATION1", then the
program would go to OPERATION1 if the ErrorLevel was 1 or any number
higher, including 2, 3, and 255. By testing in reverse order, we catch
the higher ErrorLevels before testing for the lower ones.

This program was modeled after a program of the same name that appeared
in a magazine (I don't remember which issue). Their QUERY had one major
disadvantage; every time it ran, it would print the question and *beep*.
When you run batch files containing QUERY often, that beep can get very
annoying. Their version was also limited to about 6 possible replies,
whereas this one can handle up to 127.

Version 1.0 of this program was written by Michael B. Smith in Turbo
Pascal. It was written in the Fall of 1986, and revised slightly in
July of 1987.

Version 2.0 was written in assembler by Richard Wiersma on August 13,
1987. This version was only 95 bytes long (instead of 12K), and it acts
a little bit differently:

1) Some copies of QUERY version 1 would make the screen go blank or
all green on an EGA display (and maybe on a normal color display too).
This seems to be due to a bug in Turbo Pascal. QUERY version 2 does
not have this problem.

2) QUERY v1 would print its messages in a different color on color
displays; on a monochrome display, the text would be printed in bold.
QUERY v2 prints its messages with the default color.

3) If there were any spaces on the command line after the "@", QUERY
v1 would ignore them. That meant that a space could not be a valid
response. QUERY v2 treats any space after the "@" as another possible
response, just like any other character.

4) If an invalid key was hit as a response to the question, QUERY v1
would ignore it. The program would not proceed any further until a
valid key was hit. QUERY v2 will proceed, but the ErrorLevel will
be set to 255.

5) If there is no "@" given on the command line, QUERY v1 would beep
and print "Invalid parameters". The ErrorLevel would be set to 0.
QUERY v2 will just end with the ErrorLevel set to 255.

6) If there is an "@" on the command line, but no list of valid
responses given, QUERY v1 would beep and print "Invalid parameters".
QUERY v2 will wait for a response, but it will return an ErrorLevel of
255 no matter what key was hit. Remember that an "@" with a space after
it is not considered an error by v2; the space will be considered the
valid response.

You are allowed to copy, distribute, modify, and use this program freely,
as long as it is not sold for profit. Enjoy! Michael Smith August 13,
1987

Version 3.0 uses Mr. Wiersma's assembler code with modifications by
James L. Wargula on April 22, 1989. The length has been increased to
104 bytes.

1) The program was changed back to version 1 style, that is, to wait for
a correct response, rather than ending with an errorlevel 255 upon a
non-valid response. This has the effect of speeding up the operation
when a mistake is made. The menu screen stays there rather than having
to be repainted.

You are still allowed to copy, distribute, modify, and use this program freely,
as long as it is not sold for profit. Enjoy! James L. Wargula April 22, 1989.


 December 12, 2017  Add comments

Leave a Reply