The magazine of the Melbourne PC User Group

Batch File Basics
Tom Coleman

Have you noticed how instructors and writers casually toss off phrases like "Edit the line in your Autoexec.bat", "Just write a batch file" and so on. For the beginner or the less-than-confident the prospect of editing or writing a file is an daunting idea.

Contrary to some peoples expectations these files are not written with toad bile gathered at the full moon. They are written with ASCII (pronounced ASS-KEY, the initials of American Standard Code for Information Interchange) text editors which is, at first glance, even blacker magic.

As a general rule you cannot write batch files and so forth with a conventional word processing package. There is one exception, that is PC-Write. The reason you cannot use most word processors to write DOS files is because the word processors put in lots of extra characters, like "tab", "carriage return", "bold" and so forth. generally they are instructions to the printer or reminders that the program puts there for housekeeping.

DOS cannot cope with these extra characters. It blunders along trying to execute them and usually hangs or panics or gets the sulks.

Most of the word processors have the ability to produce ASCII text. For reasons best known to programmers, who love to make computing confusing, all the different word processors give ASCII text different names. One calls it unformatted text, another calls it plain text and there is non-document mode and so on. Different names for the same thing.

To write files to be used by DOS you must have an editor that produces none of these extra characters. There are plenty of them around. We all have EDLIN that comes with DOS.

EDLIN has lots of knockers who decry it for what it does not do. They miss the beauty of its simplicity. You can write batch files for ever using five commands. Everyone can learn in 20 minutes all they need to know about EDLIN to last them for the rest of their lives. After that it is only a question of practice.

There are other very good editors. PC-Write, Blackbeard and SPC are all very good. Much more sophisticated than poor old EDLIN and certainly the preferred choice if you are creating more than a page of text.

There are text editors built in to Xtree, Norton and PCTools, all of which turn out pure ASCII text. You can use these to create or modify your files.

DOS 5 comes with two editors. EDLIN and a cut-down version of the editor that comes with Quick Basic. They call it EDIT. It is very nice. Pull down menus and all the accoutrements of what is trendy in computing. It is thoroughly recommended.

So you see there are plenty of tools around to write batch files with. However none of them will write a file for you. You must do that yourself.

Getting to know how to use the editor is the first step in writing files. The next bit is writing the file. After that you can execute it and then debug it before you start to improve on it.

Let's see what is involved in writing a simple batch file.

First of all lets define what is a batch file.

A batch file is a file with a .BAT extension. DOS recognises these files as containing a series of commands, similar to the commands that you would otherwise enter from the keyboard.

For now we will let that definition stand. Actually there are special batch file commands that give you better control over the way the batch file executes but they will come later.

If you write a file with a .BAT extension that does not contain commands then you have a batch file that does not work. DOS thinks it is a batch file.

What is usually involved in starting up a program from the DOS Prompt?

For the purposes of this exercise we will start up WordPerfect.

First you would make sure you were in the right drive. Lets say you want to change to drive c:. You would enter:  
C:

If you were already in drive C: then nothing would happen. No harm done but useful if you were in any other drive.

You probably know that changing drives does not always change you back into the root directory. You usually return to the directory you were in when you last left that drive.

Having changed drives there is no saying what directory you might be in so you would have to enter:
CD\WP51

to get into the WordPerfect subdirectory. Of course you could have entered CD\NORTON and got into Norton or CD\XTPRO to get into Xtree or any other subdirectory depending on what you want to do.

Now you would enter.

WP

and WordPerfect fires up.

You could enter those commands from the keyboard or write them into a batch file. Let's call it WP.BAT. You could call it anything you like so long as it has a .BAT extension. ABC.BAT would work just as well but the name does not indicate its purpose, so is less intuitive.

Fire up your editor with a new file called WP.BAT. Most editors want the name when they start. Enter three separate lines with the above commands, one per line.

C:
CD\WP51 
WP


Ifs a good idea to put this file somewhere where you can find it. In one of the subdirectories in your Path is the best place.

Any time you execute this batch file with the command WP (or ABC if you are bloody-minded) it will execute those commands and fire up WordPerfect.

Variations on this theme will give you batch files that start up most of your application programs.

Batch files do not stop executing until DOS sees there are no more commands left to execute. What this means is that all the time that you are running WordPerfect the WP.BAT is sitting in the background twiddling its thumbs. When WordPerfect finishes running, control is passed back to the batch file, which having no more commands in it, passes control back to DOS.

This could be a golden opportunity to add more commands to backup your work. Think about it.

Why wait until the batch file is finished to add something. Why not use the batch file to load up your other support TSRs. Do you use GRAB? You could load it up in the same batch file before the WP start up commands.

You could flash up a message to make sure that the printer is turned on or has the right size paper.

A bit of imagination goes a long way. The mind boggles.

There are plenty of batch files around that do these simple tasks. There are some very sophisticated batch files that perform voodoo as well. Read them. Study them Pick the brains of others who have been there done that.

If you get stuck talk to someone.

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

[About Melbourne PC User Group]