The magazine of the Melbourne PC User Group

Interactive Batch Files
Russell Langley

Interactive programs ask you questions and respond appropriately to your answers (e.g. "if yes, do this, else do that"). The 'Batch File Language' consists of Dos commands that COMMAND.COM can interpret It is a primitive language, but strong enough to automate many straightforward sequences of commands, like those in your AUTOEXEC.BAT file.

Batch files achieve limited flexibility because they can accept up to nine additional terms (parameters) entered on the command line after the batch filename (e.g. 'MYBATCH Tuesday B:' where Tuesday and B: would be two parameters). Once started, however, they can only respond to questions by pausing and offering to quit if you press Control+Break.

To provide truly interactive batch files, therefore, requires help from .COM or .EXE programs not supplied with your DOS files and which are designed specially to ask questions and present batch files with answers that they can understand. You can then include these extra utilities in critical positions anywhere in batch files, with remarkably enhanced usefulness. These 'questioner' programs are of two kinds, as follows.

1. Single Key Press Detectors

These wait until a key is pressed, and then do one of the following:
  • Set an errorlevel value which the batch file can evaluate, for example by "if errorlevel 4 goto Label4", or
  • Save an empty file whose name is the key pressed, on the active disk, to be recognized in the batch file by, say, "if exist P goto LabelP", or
  • Create a named variable in a reserved part of RAM called the 'environment', to be interpreted then by, say, "if %choice% == T goto LabelT".
2. Entered Phrase Recorders

These wait until you've typed one or more words, and pressed Enter. They will then save what you've typed as an 'environment variable'. Such variables are distinguished by having their name enclosed in percent signs (for example, '%choice%'), and as such are available to all future programs in that session, or until they're erased (like 'set choice=').

Many of these questioner programs are very cheap (they can be found in the Public Domain) and very valuable (because they make much better batch files). Some examples are:
  • BATCHMAN.COM by Michael Mefford, has 50 enhancements including GETKEY. See Australian Personal Computer, February 1990, pages 163 - 179 
  • GETLETR.COM is a small program you can make with DEBUG. See Paul Somerson, DOS Power Tools (Bantam Books), 1st edn page 614, or 2nd edn page 618
  • GETVAR.COM, by Roger Plant, is a small program for reading phrases which you can make with DEBUG. See PC Update, November 1990, page 35
  • BATUTIL.COM, by CtrlAlt Associates, is a Shareware program with more than 100 enhancements. See Dale Lewallen, Australian PC User, December 1990, pages 57 - 62 
  • READKEY.COM and READLN.COM, by Ralph B. Smith Jr, a nice pair of Freeware files. READKEY reads single keypresses, READLN reads entered phrases.
The ARC.BAT Program [click HERE to download source listing]

The accompanying listing is an elegant example of a batch file which packs &/or unpacks compressed (archived) programs and data files on your hard disk, and after unpacking, offers to scan the decompressed files for viruses. It uses the above mentioned READKEY and READLN programs to ask numerous questions inside the batch file, and responds appropriately each time.

Both utilities save keyboard responses as 'environment variables' with names you provide. These variables must be removed from RAM at the end of the program to prevent running out of environment space. If ever ARC.BAT halts with the message "Out of environment space", you must enlarge that space to, say, 512 bytes by inserting the following line in your CONFIG.SYS file (with a text editor), and then rebooting for the change to take effect:

SHELL=C:\COMMAND.COM /P /E:512

Like all batch files, ARC.BAT has to be typed in a 'pure text-editor'. This means DOS's EDIT program, e or the decrepit Dos editor EDLIN, or in a word g processor (in which case you must save the file 'in ASCII format'). Before running ARC.BAT, make sure you also have the following files to be present in a directory (for 'example, C:\TOOLS) that is on your search path:
  1. PKARC.EXE and PKXARC.EXE
  2. PKZIP.EXE and PKUNZIPEXE
  3. LHA.EXE
  4. READKEY.EXE and READLN.EXE and
  5. McAfee's SCAN.EXE
Studying the commented listing will explain how it works. If you're typing it, omit all the REM (remark) lines so that it will run faster. But be warned, once you experience the power of interactive batch files you may never be quite the same again.

Reprinted from the November 1992 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]