An Introduction

What are Batch files?

Batch files are little programs that we mortals can write for ourselves. Because they are created by you and me, batch files have to be in human readable form so we can understand what we are doing. They consist primarily of a group (or batch) of DOS com mands that, when executed sequentially, perform a task we want done. These files are stored on disk in ASCII (i.e. text) format, usually in a directory by themselves, such as the C:\BAT directory. Batch files consist entirely of letters, numbers and symb ols that humans can read and understand.

What is a file?

To understand batch files you have to know a little bit about files in general. In computer terms, a file is a body of data (information) that is treated as a unit, given a filename for identification, and stored on a hard or floppy disk.

Parts of a filename:

In DOS, the name you give your file may have (but doesn’t have to have) two parts - three if you count the period that separates the other two parts. The three parts of a filename then are: The name itself - consisting of from one to eight characters, a period to separate the name from the extension if there is one; and an optional extension, which may consist of up to three characters.

Kinds of files:

There are just two kinds of files - executable and non-executable. We call executable files “program files”, and non-executable files “document files” or “data files”.

How does DOS know the difference?

DOS has to treat different kinds of files differently, so by convention, we identify executable files by means of their three-letter extensions: .COM, .EXE or .BAT. The .COM and .EXE extensions indicate program files that have been translated from sourc e code into machine language by a compiler and are stored in binary format. This compiled (binary) code, unreadable to human eyes, is the fastest format for computers to process (execute). Compiled program files don’t need to be interpreted by DOS at exe cution time as Batch files do.

The .BAT extension indicates to DOS a special type of executable file that is stored in text (ASCII) format. When you execute a file that has a .BAT extension, DOS invokes (calls on) its Batch File Interpreter which reads, interprets and executes each li ne of the batch file sequentially as it is encountered. In other words, batch files are interpreted at execution time rather than compiled beforehand. Because this is such an incredibly slower process than executing object (binary) code, batch files are not considered suitable for long programs.

The Language of Batch Files

The DOS Batch file interpreter can process:

1. All the DOS commands, both internal and external, such as DIR, TYPE and FORMAT.

2. All the DOS redirection symbols - pipe (|), greater-than (>), less-than (<) and any device names that they address (PRN, CON, NUL, AUX, etc.)

3. Eight special batch file commands - Call, Choice, Echo, For, Goto, If, Pause, Rem, and Shift.

4. Two special characters - only meaningful in batch files:

The at-sign (@) which cancels display of a command line, and

the percent sign (%) which is used two different ways, single and double.

A single percent sign (%) followed by a number (%1, %2, etc.) refers to a replaceable parameter, which may be thought of as a place holder in your batch file.

A double percent sign (%%) with the name of an environment variable between them refers to the contents of that variable. (%path%, for example, refers to the contents of the PATH variable).

5. Names of applications (programs) - such as WIN to execute WINDOWS, or WP to execute WordPerfect.

6. Names of other batch files.

Another batch file may be executed directly by typing its filename on the command line. This will execute the object batch file but does not return to the source batch file.

Another batch file may be executed indirectly by typing “CALL” followed by the filename. This will execute the target batch file and return to the next instruction in the source or calling batch file. Called batch files can also call other batch files. This is called “nesting.”

The Nine Special Batch file Commands and what they do

CALL - “CALLS” and executes a batch file, then returns to the next executable instruction in the source (or calling) batch file.

CHOICE - Allows keyboard selection of menu items or alternatives.

ECHO - Displays messages on the monitor (may be redirected to other devices, such as a file on disk, or the printer).

FOR - Permits iteration through a stated or implied list of values, each “hit” resulting in the execution of a stated command.

GOTO - Jumps (skips) to a label within the batch file. A label can be placed anywhere in the batch file, and consists of a colon (:) followed by a name. For example, “GOTO REPEAT” causes the next instruction executed to be the one that follows the label “:REP EAT”.

IF - Tests whether a stated condition is true or untrue. IF and IF NOT are usually followed by a GOTO command for either the true or the false condition, with straight sequential (fall thru) execution on the default condition.

PAUSE - suspends processing, displays the message “strike any key when ready” and proceeds to the next instruction when a key is struck.

REM - Allows insertion of remarks for your own or the user’s benefit. Tells the batch file interpreter to ignore everything in the line that follows.

SHIFT - Used with replaceable parameters. Causes right-to-left parameter replacement. The left-most parameter will drop off, and the others will shift one position to the left.

Evelyn Brown is a HAL-PC member and Saturday DOS Instructor/Coordinator.


E-mail me at webmaster@hal-pc.org with any comments you have and tell me what you want to see here.

Back to the Magazine Home Page