DOS’s Killer Application—Batch Files

By Carolyn Z. Gillay and Bette Peat

Adapted from “Windows User’s Guide to DOS” by Carolyn Z. Gillay and Bette Peat (published by Franklin, Beedle & Associates).This is an excerpt from chapter 10; you can download the PDF file for the entire chapter at the Franklin, Beedle page for this book.

Using Edit to Write Batch Files

To write batch files, you need a mechanism to create an ASCII text file, since that is what a batch file is. You should remember that ASCII is a code used by the operating system to interpret the letters, numbers, and punctuation marks that you key in. In simple terms, if you can read a file with the TYPE command, it is an ASCII text file. You can use a word-processing program to write a batch file if it has a nondocument or text mode. However, most word-processing programs are quite large and take time to load into memory. Most batch files are not very long, nor do they need the features of a word processor. Using a word processor to write a batch file is like using a sledgehammer to kill a fly.

Having a small, simple text editor is so important that the operating system includes one as part of the system utility programs. This is the MS-DOS editor, called Edit. Edit is simple to use and universal. You will write some batch files using Edit. Remember, Edit is only a tool to create the file; it does not run or execute it. You execute the file when you key in the file name at the system prompt in the MS-DOS window. Each line in a batch file must contain only one command. A batch file can have any legal file name but the file extension must always be .BAT.

If you are in the Windows interface, the text editor is Notepad. Like Edit, Notepad creates text-only files and may be used to write batch files. However, if you are having problems with Windows, you will not have Notepad available to you because you need a graphical user interface to use Notepad. Edit, on the other hand, can work at the command line. In fact, you will later see that when you create your startup disk, Edit is on the disk, but not Notepad. Thus, in the following activities, you will be using Edit.

Activity: Writing and Executing a Batch File

Note 1: The DATA disk is in Drive A. A:\> is displayed.

Note 2: Although you may use a mouse with the MS-DOS editor, the instructions will show the keystroke steps, not the mouse steps.

Note 3: The amount of space shown as remaining on the disk will vary, depending on the size and placement of the batch files on your disk.

Step 1 Key in the following: A:\>EDIT EXAMPLE.BAT <Enter>

What’s Happening? You are now using the MS-DOS editor. You are going to create a batch file named EXAMPLE. The file extension must be .BAT.

Step 2 Key in the following: DIR *.99 <Enter>

Step 3 Key in the following: DIR C:\*.99 <Enter>

What’s Happening? Look at each line. Each one is a legitimate operating system command that could be keyed in at the prompt. Each command is on a separate line. The first line asks for a listing of all the files on the disk in the default drive that have the file extension .99. The second line asks for all the files in the root directory of C that have the file extension .99. At this point, you have written the batch file. Next, you need to exit Edit and save the file to disk.

Step 4 Press <Alt>+F

Step 5 Press X

Since you have not saved the file, Edit reminds you with a dialog box that, if you want this file on the disk, you must save it.

Step 6 Press Y

A:\>EDIT EXAMPLE.BAT
A:\>_

What’s Happening? You have saved your file, exited the MS-DOS editor, and returned to the system prompt.

Step 7 Key in the following: A:\>DIR EXAMPLE.BAT <Enter>

A:\>DIR EXAMPLE.BAT
 Volume in drive A is DATA
 Volume Serial Number is 2415-16DD
 Directory of A:\
EXAMPLE  BAT            23  06-27-98 10:43a EXAMPLE.BAT
         1 file(s)             23 bytes
         0 dir(s)         1,250,304 bytes free
A:\>_
    

What’s Happening? The DIR EXAMPLE.BAT command shows that there is a file on the DATA disk called EXAMPLE.BAT. It looks like any other text file.

How do you make the operating system treat it like a program so that you can execute it? You simply key in the name of the file at the prompt. You do not need to key in the extension, just the name. The operating system first looks for a file in its internal table called EXAMPLE. It does not find it. It then looks for a file called EXAMPLE.COM on the default disk, the DATA disk. No file exists called EXAMPLE.COM. Next, it looks for a file on the default disk called EXAMPLE.EXE. No file exists called EXAMPLE.EXE. Finally, the operating system looks for a file called EXAMPLE.BAT on the default disk. It does find a file by this name. It loads it into memory and executes each line, one at a time. Thus, to execute the batch file called EXAMPLE, key in the name of the file at the prompt. Watch what happens on the screen after you key in the file name.

Step 8 Key in the following: A:\>EXAMPLE <Enter>

A:\>EXAMPLE
A:\>DIR *.99
 Volume in drive A is DATA
 Volume Serial Number is 2415-16DD
 Directory of A:\
JAN      99             73  10-10-97  4:21p JAN.99
APR      99             72  10-10-97  4:21p APR.99
         2 file(s)            145 bytes
         0 dir(s)       1,250,856 bytes free
A:\>DIR C:\*.99
 Volume in drive C is ADMIN
 Volume Serial Number is 423E-16F9
 Directory of C:\
File not found
                    1,385,299,968 bytes free
A:\>
A:\>_

What’s Happening? The operating system read and executed each line of the batch file you wrote, one line at a time. The screen displayed each command line and the results of the command line as it executed. Each line executed as if you had sat in front of the keyboard and keyed in each command individually. You did key in the commands when you wrote the batch file, but you had to key them in only once. The first line was DIR *.99. When the operating system read that line, it executed it and showed on the screen both files on the DATA disk with the file extension .99. It read the next line of the batch file and looked in the root directory of Drive C for any file that had the file extension .99. Since there were no files on that drive with the extension .99, it gave the message File not found. Now that you have written the file EXAMPLE.BAT, you can execute this batch file’s commands over and over again by keying in EXAMPLE at the prompt.

 

Go back to eTechNotes main page.