< Previous  |  Contents  |  Next >

10 : DOS Plus Batch Files        


What are Batch Files?

One of the most powerful features of DOS Plus or MS-DOS is the ability to automate sequences of commands. You may have already come across this in principle on the BBC micro, where the *KEY command is sometimes used to simulate the pressing of ten or twenty keys, including RETURN. For longer sequences an EXEC file is used. This command (*EXEC) takes a normal text file, such as might be saved from a wordprocessor or text editor, and treats all the text in the file as keyboard input. In other words, it behaves as though you had typed in the text (ie commands) directly.

The benefits of storing commands in this way are most apparent when the command sequence will need to be repeated from time to time. When stored in a file which is known to be correct, the sequence can be repeated on demand, with guaranteed results and no possibility of new errors creeping in. It is also rather easier than repeated retyping.

DOS Plus provides these facilities by means of batch files. These are files which, as above, contain commands of the type you would enter through the keyboard, for example to catalogue discs or to copy files. In practice you can regard batch files as a method of defining your own completely new automated functions. All normal DOS Plus commands, both permanent and transient, can be used together in any sequence, and can carry out any operation you would be able to control from the DOS prompt, including calling a batch file!

A DOS Plus batch file (always recognised by the '.BAT' extension) is essentially similar to the EXEC file on the BBC, although it is rather more powerful, having, as it does, a language structure to permit control of its actions from within. The batch language is simple but powerful, and provides for control of execution by means of 'IF' and 'GOTO' statements. The action of these can be altered by external factors such as, for example, the presence or absence of a specific file.

Another major difference between DOS batch files and BBC EXEC files is that a batch file can run a complete applications session from beginning to end. On termination of the application the batch file can still be in control. This means that applications actually can be run from within batch files, rather than simply being started by them as on the BBC. In addition, as noted above, one batch file can run another. This facility, together with the ability to execute programs and make variable decisions as execution proceeds, provides the DOS user with a comprehensive and powerful job control language which is simple to implement.

Here is a very simple example of a batch file, or is it a list of manual commands?

COPY *.txt A:
COPY *.doc A:
CD \

These commands, if entered from the keyboard, could be used to back-up text and document files from a wordprocessing directory called 'WP'. All files with a '.TXT' or a '.DOC' extension would be backed up to drive A, then any '.BAK' files (old versions of the files) on the current drive would be deleted to free the disc space. Finally the current directory would change to the root directory. These commands would have exactly the same effect if stored in a batch file. They would look the same, and would work in the same way.

It is much easier to group these commands into a batch file called, say COPYALL.BAT, than to use them manually. To execute them, all you would then need to do would be to type the command COPYALL at the DOS prompt, and all operations would be automatic.

Of course, as it stands, the above example is completely inflexible, it will always carry out the same operations on the same groups of files. However, further facilities are provided to allow variable information to be supplied to batch files, in much the same way that parameters are be supplied to manually entered DOS commands. Again, the use of these parameters is exactly the same in batch files as in manual commands.

One special batch file name is reserved by DOS, which looks for this file whenever the system is loaded. The filename is 'AUTOEXEC.BAT', and if present on disc will be executed without intervention as soon as DOS has loaded. In this way defaults for your own system, such as path names, screen colours and so on can be actioned immediately as part of the startup process.

The remainder of this chapter explores the development and use of the various facilities in batch files, providing examples of how they are constructed, what they do, and how the automated control facilities are implemented to vary the function of a batch file.

Creating a Batch File

A batch file can be created either by a line editor, such as ED, directly from the keyboard using COPY, or most easily by using your wordprocessor.

For very simple examples you may wish to enter directly from the keyboard. To do this directly you would use the COPY command. For example:


This tells the computer to copy all the input from the keyboard, line by line, to a file called COPYALL.BAT. You then type in each command line, ending with a RETURN. Finally, after all commands are entered, type CTRL-Z, followed by another RETURN to terminate input. A ^Z symbol will appear on screen but don't worry about that, nothing superfluous is added to the file. The command file will then be copied to disc. As is usual with COPY, any existing COPYALL.BAT file would be overwritten, or if it didn't exist before it would be created.

Batch files are most easily created or amended in your wordprocessor, and this becomes more useful when you start to develop longer batch files. Note, however, that your wordprocessor should not include format control codes in the document, like WordStar and some others do. The presence of these would cause the batch file to fail at execution time. If this is the case, but there is an 'ASCII only' edit option, or an ASCII save option you should use that. Failing that, you could create the file in your wordprocessor and use ED finally just to tidy it up. ED is actually very easy to use, but it must be admitted it's not very user friendly. As a last resort you can create the file in native BBC mode, perhaps using View or similar, and then use 'GETFILE' to transfer to DOS Plus for final testing and editing.

Batch File Parameters and Variables

As mentioned, batch files can be supplied with variable information, typically filenames to be used during copying, deleting, loading and so on. For example, the COPYALL example above could be made much more flexible and general purpose by supplying the directory and filenames to be used, instead of having them fixed.

This is simply achieved by stringing the required names on to the end of the batch file name, separated by spaces, when it is first called. These 'parameters' can then be substituted at appropriate points within the batch file, and the entered names will be used in any operation.

You may think this defeats the object of keeping commands as simple as possible, but there is a trade-off between simplicity and flexibility. It is finally your choice to decide how best to use the facilities in the light of your your own needs and applications. In practice, supplying parameters to batch files is no more difficult than supplying them to manual commands.

A batch file can use up to ten parameters simultaneously. Although, in total more can be entered and used, only ten have names, so only ten can be referenced at once. The ten names are simply the numbers zero to nine, prefixed by a percentage symbol, so they are %0, %1, %2 and so on up to %9. The first of these, %0, always defaults initially to the name of the batch file itself, though you can change this later, as you'll see. The other parameters are numbered sequentially from one to nine, and any parameters entered when the batch file was called are substituted, in sequence, completely automatically.

Suppose we wanted to amend the COPYALL example used earlier, to allow the directory and the file extensions used to be variable, and specified when the batch file is called. It would now be written like this:

CD %1
COPY *.%2 A:
COPY *.%3 A:
ERASE *.%4
CD \

When we call this routine, it will now expect four variables to be supplied (%1, %2, %3 and %4). These are, in order, the directory, the file extension for the first and second COPY operations, and the file extension for the ERASE at the end. Note that we can still leave some items fixed, like the destination drive and the reselection of the root directory. Although these too could be replaced by parameter substitution, only variable items need be substituted.

If we were to call COPYALL now, and wanted to COPY files with both '.IDX' and '.FIL' extensions in directory '\DATABASE\DATAFILE', finally deleting any files with a '.WRK' extension, our entry would be:


All you need to take care of is the order in which you enter the parameters, separating each from the next by a space.

Of course, even when jobs are controlled by batch files, errors can still crop up such that you need to stop the job at once. Any batch job can be terminated at any time simply by pressing CTRL-C. DOS Plus will automatically request confirmation, to which the usual Y or N response is required. The message takes the form:

Terminate batch job Y/N ?

Naturally if you answer 'N' execution continues, but if you confirm, all open files are closed, all operations are abandoned and you are returned to the normal DOS prompt. Note that your ability to enter CTRL-C may, at times, be restricted by the program currently running.

Batch Files and Discs

When a batch file is used to control a job, one constraint is that the batch file must remain on a currently available disc. If you are running the system from a hard disc this need not be a problem, but with only two floppy drives options are more restricted. Placing the batch file on your applications disc is one answer, but if you have also copied several necessary transients to this disc too, lack of disc space can soon become a problem.

A better solution, memory permitting, is to set up a RAM disc and copy both the batch file and the transient commands on to it. Remember too that you can set 'PATH' to point to the RAM disc, overcoming directory or drive name problems at the same time. As one batch file can call another, it is quite possible to have one batch file set up the RAM disc and copy all necessary software and batch files to it, before finally calling an appropriate batch file to start the required application if any.

< Previous  |  Contents  |  Next >

About the Master 512 | Bibliography