Classic Computer Magazine Archive Article from Compute! magazine

Batch Files With IBM BASIC

Lawrence H. Bannister

Anything that a PC-DOS batch file can do, a BASIC program can do better. By calling DOS from BASIC, you can perform many functions that cannot be done with the limited language of batch commands. The demo program below works on any IBM PC with BASICA and DOS 2.1 or later.

Most IBM users already know that you can save a lot of time by using the batch commands of PC-DOS to perform a sequence of DOS commands automatically. But the austere language of DOS provides only three variations of one simple IF statement and has no practical way at all of manipulating strings or performing arithmetic. It's very difficult to write a batch file that creates neat screen displays, makes logical branches, allows user input, and traps errors.

A more flexible technique is to call DOS commands or even batch files from within a BASIC program. This frees you from the limitations of batch files and takes advantage of the string and arithmetic functions of BASIC.

You can call DOS from BASIC as often as you wish by using the SHELL command found in IBM BASICA. Although it is not documented, this command is implemented in version 2.1 or higher of PC-DOS. Aside from a few small problems to be avoided, its possibilities are limited only by your imagination.

(Note: SHELL is also found in PCjr Cartridge BASIC, but does not seem to work reliably due to memory conflicts. Therefore, these techniques aren't recommended for use on the PCjr.)

The SHELL Game

To demonstrate some of these possibilities, Program 1 below is a BASIC program that displays two menus of options, interprets the user's responses, and then calls a variety of DOS routines in several different ways. Program 2 is a short batch file that is required as part of this demonstration.

When you run the BASIC program, it shows a menu offering four choices:


1. Show system date

2. Show system time

3. Show system date and time

4. None of the above

Enter your choice:

When the user presses a key, the program checks to see if the keypress was 1, 2, 3, or 4, and if so, uses the SHELL command to call the appropriate DOS function: DATE, TIME, or a batch file (Program 2) that calls both DATE and TIME.

When DOS returns control to BASIC, Program 1 displays a second menu:


1. Run Checkdisk

2. Show Disk Directory

3. None of the above

Enter your choice:

This is similar to the first menu, except this time the program calls a DOS function that requires a parameter to be passed to the DOS command line. The BASIC program asks the user for the necessary information, then concatenates the appropriate command-line string.

Notice that the SHELL command can pass either a literal string, as done in the first menu, or a string variable, as in the response to the second menu.

No Recursion Allowed

There are two considerations to keep in mind when using this technique. First, make sure your system has enough memory. Although DOS, BASICA, and your BASIC program can be loaded into a machine with as little as 64K of Random Access Memory (RAM), you won't have much memory left over to do anything very useful. At least 92K RAM is desirable, because DOS and BASICA together use about 90K if that much is available. You need still more memory if you also want to run a batch file that calls a lengthy program like EDLIN.

Second, be sure not to create a sequence that is reentrant or recursive. For example, the result will be unpredictable if your BASIC program calls a batch file that, in turn, calls BASIC. Reentrant sequences of this nature are apt to cause a system crash that can be remedied only by turning off the power.

A minor aggravation is that DOS scrolls 25 lines on the screen while BASIC scrolls only 24 lines due to the function key display on the 25th line. Furthermore, BASIC and DOS each maintain an independent pointer to the screen position of the cursor. These differences can cause BASIC PRINT statements to overwrite something that DOS has just printed.

To avoid this problem, always start the BASIC program with the KEY OFF command to turn off BASIC's function key display. Then use a CLS (clear screen) command each time that DOS returns control to BASIC, or, as shown in the sequence following the second menu in Program 1, surround the SHELL commands with LOCATE 24,1 statements and two blank PRINT lines to ensure that both DOS and BASIC always start scrolling from the bottom of their own screens.