Classic Computer Magazine Archive COMPUTE! ISSUE 132 / AUGUST 1991 / PAGE 60

Beautiful batch files. (batch file system)(includes related articles)
by Tony Roberts

Since Windows 3.0 hit the streets, it's almost embarrassing to be caught operating your PC from the old DOS prompt.

Let's face it, though. There are millions of PCs out there for which Windows 3.0 is not a practical option. They'd need more memory, more speed, and more hard disk space than the budget will allow for anytime in this century.

But don't despair. With a little work, a little creativity, and a handful of utilities from this issue's COMPUTE's PC Disk, you can put some snap in your DOS interface and make your PC system easier for you and your whole family to use.

This article shows you how to create a batch file systems that you can use to

* Run programs from menus rather than from the DOS prompt.

* Instruct your system to behave differently depending on who's running it.

* Create and display attractive menus and help screens.

* Include musical signals to let you know when an operation is completed.

One of the problems with DOS's batch language is that it's missing a few important features that would make it practical for a powerful menuing system.

Problem Number 1

The main deficiency with DOS's batch language is its inability to accept and act upon user input. Solving this problem is relatively easy using a stand-alone program that accepts user input and passes an exit code back to your batch file. In fact, such a program is the centerpiece of any enhanced batch file programming and is demonstrated in the program MENUDEMO.BAT that accompanies this article.

Several versions of this type of utility are available under names like GETKEY.COM, ASK.COM, or CHOOSE.COM. COMPUTE's version of GETKEY, which is included on this issue's PC Disk, reports back to the batch file the ASCII code of a key pressed by the user. The MENU program, also on the disk, provides another method of accepting user input.

If you don't have COMPUTE's version of either GETKEY or MENU but do have a similar program that accepts user input for use in batch files, you should be able to adapt it to MENUDEMO.BAT with little difficulty.

Once your program is able to accept commands from the user, you can use the other programs on the disk to make your batch programs sparkle with everything from colorful text and boxes to music.

Variable Blues

Another problem batch programmers face is that there's no straightforward provision for using variables. This difficulty can be circumvented, though, by using environment variables to keep track of changing information. MENUDEMO.BAT shows how to capture user input, act on it, and store and retrieve environment variables. This program, although large by most batch file standards, doesn't do very much itself. It's simply a shell you can use to build your own program based on the characteristics of your system.

You'll notice that this batch program uses labels and GOTO statements liberally and keeps the entire program in one file. This keeps things simple. It saves you the trouble of remembering dozens of batch file names, and it can save quite a bit of disk space.

On a typical hard disk, where each file uses a minimum of 2,048 bytes of disk space, ten 100-byte batch files would consume 20,480 bytes while one 1,000-byte batch file would use only the minimum 2,048 bytes.

Faster Floppies

If you run batch files from a floppy disk, however, it might be wise to break your menu system into several small files to improve execution time. If you have DOS 3.3 or above, you] can use the CALL command to execute one batch file from inside another.

Why is this faster? When batch files are run, the disk is read as each program line is executed. A large batch file, run on a floppy disk, will be very slow because DOS will slog through the file line by line looking for the appropriate section.

Note also that the batch file extensions demonstrated in the program have some overhead themselves. When you execute a PUTTEXT command, for example, the PUTTEXT program runs, delivers your message to the screen, and then returns control to the batch file.

All of this activity takes time. If you want really fancy programs and plan to use several BOX and PUTTEXT statements, you'll be better off building your screens and savings them with SAVESCRN. Then your batch files can use LOADSCRN to load in the screen files faster than redrawing them.

Finally, if you don't have a hard disk, you can get the best performance out of this type of system by installing a RAM disk, copying the batch files and the batch file extension programs to the RAM disk, and running everything from the RAM disk.


By way of a quick walk-through of MENUDEMO.BAT, the program is an assembly of several sections, each of which is labeled. Each section ends with a GOTO command to transfer control to another section. The program keeps returning to the main menu section until the user chooses to exit to DOS.

MENUDEMO finds checks to see if an environment variable called user exists. If not, the log-on section is executed, a menu of valid users is displayed, and the user variable is established.

Next, the main menu is displayed, and choices pertinent to the current user are presented. From there, the program acts on any selections made by the user and returns to the main menu when finished.

Note that one of the options in every user's menu is Log In New User. This makes it easy to switch from user to user without having to reboot or rerun the program.

Once you get going with this system, carry it as far as you like: Design separate menus for each member of the family rather than filling in one generic template as this program does. Introduce a games submenu that's accessible from every user's main menu. If you telecommunicate, create a submenu that runs scripts to log you on to all your favorite services with the touch of a key.

If you've stayed with me this far, you probably enjoy tinkering with your computer as much as you enjoy running software. The other members of your family, however, probably want results, not challenges.

They'll appreciate being able to turn on the computer and run their programs without having to call on you to find the correct subdirectory, look up the proper parametersm reset the printer, load the printer driver, and on and on and on.

Tuborcharge Your Batch Files

In the predecessors of today's PCs, batch processing was a method of instructing the computer to carry out tasks without operator interverntion. Long printing tasks and other time-consuming business was scheduled to run unattended late at night when drowsy humans were sleeping and wakeful computers were looking for some action.

As DOS developed, the batch programming language it incorporated was designed to accomplish the same goals--getting work done without operator intervention.

PC users, however, did not behave like mainframe programmers. Rather than using batch programs to handle work while the operator is away, most PC users employ batch files to carry out repetitive tasks while they're sitting right in front of the screen.

Whereas no one cared what the display on the mainframe console looked like, PC users want boxes, colors, noises, and otherwise interesting and informative screens.

To make the batch language conform better to our needs, COMPUTE has developed a handful of small programs to provide the visual excitement and interactivity that normal batch programs lack.

These programs, BOX, GETKEY, LOADSCRN/SAVESCRN, MENU, NOTES, PUTTEXT, and TIMEDATE, are all available on this issue's PC Disk. (These programs and more can also be found on COMPUTE's PC Productivity Manager

Musical Notes and Frequencies

Note Frequency

A 55 110 220 440 880

A# 55 117 233 466 932

B 62 123 247 494 988

C 65 131 262 523 1046

C# 69 139 277 554 1109

D 74 149 294 587 1754

E 82 165 330 659 1319

F 87 175 349 698 1397

F# 93 185 370 740 1480

G 98 196 392 784 1568

G# 104 208 415 831 1661

By using NOTES, you can program your PC's

speaker to play something more lively than the

usual beep. This chart provides frequencies for

five octaves of musical notes. For reference, the

frequency of middle C is 262.

disk, a collection of 38 batch file extensions and power utilities.)

Here's a rundown of how to use these batch file extensions.

BOX--Draw boxes anywhere on your screen using this utility. You control the size, shape, border type, and color. To use the program execute the command

BOX ror column width height color


Valid values for row are 0-24, and this indicates the row number for the top of the box. Column, which can be from 0-79, indicates the column number for the left side of the box. Width and height define the size of the box in columns and rows, respectively. To determine a color value, multiply the background color by 16 and add the foreground color. (The accompanying Color Selection Table will make this task a little more convenient.)

To select a border, enter 0 for no border, 1 for a single-line border, and 2 for a double-line border. If you enter any other character, that character will be displayed as the border.

GETKEY--This program is important for setting up interactive batch files. First display a menu of choices; then enter the command GETKEY. The batch program pauses until a key is pressed.

GETKEY returns an exit code that can be tested with the IF-ERRORLEVEL command to determine which key was pressed.

The exit code returned is the ASCII value of the character pressed. (See "Understanding ERRORLEVEL" for more details.)

LOADSCRN/SAVESCRN--This pair of programs makes it possible to build screens ahead of time and display them in a flash. This is especially helpful if you're running your batch files from a floopy disk. Batch files are slow-running programs because the system keeps going back to the disk to read and execute each line.

Creating elaborate screens full of boxes and text requires several lines of programming and takes several seconds to display.

To use these utilities, write a batch file that creates your screen and then, as the last line in the batch file, issue the command SAVESCRN filename. The screen display will be recorded as a file on your disk using the filename you provide.

Later, when you want to display that screen as part of a batch file, issue the command LOADSCRN filename, and all the boxes and text will be loaded quickly with a single command.

MENU--This utility makes it easy to create, display, and use menus. You can make menu selections with the keyboard or the mouse. To set up a


menu, enter the command:

MENU T = title, M = [M]enu Item 1,M = [M]enu Item 2, and so on.

You can enter several menu items, placing the hot key for each item in the square brackets. For example:

Menu T = SYSTEM OPTIONS M = [C]HKDSK,M = [F]ormat Drive A,M = [W]ord Processing

When a menu selection is made, an exit code is returned that can be tested with IF-ERRORLEVEL, and the program can branch to the appropriate section. If the first menu selection is picked, an exit code of 0 is returned; if the second menu selection is chosen, an exit code of 1 is returned; and so on. IF Cancel is chosen, either by clicking on the cancel box or by pressing Esc, an exit code of 255 is returned.

NOTES--This program elicits something a little more interesting from your computer's speaker than the usual flat beep. To use NOTES, enter

NOTES frequency delay [frequency] [delay] and so on.

For reference, the frequency of middle C is 262. See the accompanying chart for other values.

Delay, which specifies how long the note should be played, is measured in intervals of 1/18 of a second. Specify a delay of 18 to play a note for 1 second, 9 to play a note for 1/2 second, and so on.

A crude rendition of "Mary Had a Little Lamb" looks like this:

NOTES 330 3 294 3 262 3 294 3 330 3 330 3 330 6

PUTTEXT--Here's a big improvement over the ECHO command. With PUTTEXT, you tell DOS where you want your message to appear and in what color. The syntax is PUTTEXT row column color text. The color parameter is calculated by multiplying the background color by 16 and adding the foreground color.

TIMEDATE--Here's an easy way to add the date and time to your menus.

Just issue the TIMEDATE command by itself to get both the time and date or specify TIMEDATE T for time only or TIMEDATE D for date only.

If you use TIMEDATE with a B parameter, the display will be boxed and centered on your screen.