The basic idea of a batch file is really very ingenious. When you want the computer to perform a given task more than once, why not have the computer store the details of that task? A batch file is DR DOS's way of doing that. When you tell the computer to execute a batch file, DR DOS reads and follows that batch file just like an actor reads and follows a script. In fact, the concept of the computer following a script is an excellent mental image of a batch file for you to use through out this book.
Think of this section as a batch file "sales pitch" where I try to convince you that batch files are worth using! As we will see, batch files have four major functions:
We'll look at each of these in more detail.
Let's think about an example. When I sat down to begin writing this chapter, I had to enter some commands first:
Counting the return needed at the end of each line, I had to type sixteen keystrokes to start my word processor. As we will see, a computerized script (in other words, a batch file) can easily reduce this to as few as two keystrokes. So the first advantage of a batch file is keystroke reduction.
Reminder: Batch files can save you a lot of keystrokes.
I'll offer an example to prove this and give you your first experience with batch files. The command to get a directory from the A drive is
DIR A:*/P
We can shorten this command. Without worrying about what you're typing, enter the following at the command line prompt. Just remember that Enter means press the Enter key (which might also be labeled as Rtrn or Return), and F6 means press the F6 function key and not type "F6" from the keyboard:
COPY CON A.BAT Enter DIR A:/P F6 Enter
You should see the message "1 File(s) copied." Now put a disk in drive A and type
A Enter
If everything goes well, you'll now see a directory of the A drive. You might be thinking to yourself, "So what? I can simply type DIR A:/P when I want a directory." The advantage of A.BAT is that you can get a directory with two keystrokes instead of nine. I use a similar batch file to start my word processor with two keystroke rather than the sixteen described.
When I first started using computers, I had a great deal of difficulty remembering the command to check a disk. Instead of trying to force myself to remember CHKDSK (CHecK DiSK), I wrote a batch file called CHECK.BAT, containing the single command CHKDSK. Typing CHECK instead of CHKDSK saves one keystroke, but that isn't the primary purpose of CHECK.BAT: I can remember the word "check" much easier than I can remember the nonword "CHKDSK."
Remember: Batch files can create new commands or change the name of existing commands.
Of course, I could have renamed CHKDSK.COM to CHECK.COM and achieved the same thing, but then no one else would have been able to check a disk on my computer. Writing a batch file allowed me to use the name I wanted without interfering with normal operations.
In this simple example, "new command construction" is a very fancy name for making a command accessible under another name, much like calling a garbage collector a sanitation engineer or a salesman a purchase consultant. However, as we will see later, long series of commands can be connected to form fairly intelligent sets of commands.
Now it's time for a simple one-question test. Put away your DR DOS manual and don't look at the next figure in this book. What's the solution to the following problem?
We all know how important it is to back up your hard disk. You should be making frequent full backups, say weekly or monthly, and daily incremental backups. So write down the proper syntax of an incremental backup using the DR DOS program BACKUP.COM.
Remember: A batch file can remember complex syntax for you.
Most of you probably didn't do well on this test. Computer software often uses complex commands on the command line that are difficult for the user to remember. Happily, you don't have to. Figure out the command syntax once, and then put it into a batch file and forget it. That way, you'll never have to remember.
By the way, in case you still haven't figured it out, the syntax to make an incremental backup with DR DOS is
BACKUP C:\*.* A: /S /M
but you could easily store this in a batch file called I.BAT and just have to remember "I"-which is short for "incremental." If you can't remember that, you can pick another name. With batch files, you get to decide.
I can see you mentally waving a flag. You're asking "Hey, isn't this just another example of new command construction?" The answer is both yes and no. True, we have created a new command called "I" that is easier to remember than the string of switches previously listed, just as Check is easier to remember than CHKDSK. That statement can be made about almost every batch file we will construct in this book. However, the real purpose of this batch file was to always perform the backup consistently-without having to remember the syntax.
Most of the batch files we've looked at so far have one thing in common-they each do only one thing. They don't need to be limited to one activity, though. DR DOS keeps track of its location in a batch file even while it's doing something else. Let's do away with that one- command constraint right now. Consider this simple two-line batch file:
CHKDSK DIR
First, it runs CHKDSK. While CHKDSK is running, DR DOS maintains its place in the batch file. Think of yourself putting your finger in a book so that you won't lose your place while answering the phone. After your phone conversation is over, you can pick up reading just where you left off.
When CHKDSK finishes, DR DOS reads the next command in the batch file and runs Dir. Although CHKDSK takes only a few seconds to run, it could have been a word-processing program that runs indefinitely. It doesn't matter. DR DOS remembers and picks up the batch file whenever the program finishes.
Remember: Batch files run programs consistently.
In this past example, the batch file established a standard script to be followed each time it is run. While this is not important in this example, consider the next one:
C: CD\WORD WORD CD\ MENU
This batch file automates loading my word processor, using the steps I have just described. The first three lines are identically the commands I discussed using above when I manually start my word processor. The last two lines reload my menu program once my word processor has finished. This way, my word processor starts and terminates consistently-always starting on the same drive and in the same subdirectory and always terminating in the root directory of the C drive.
A final reason for using batch files is safety. Some of the DR DOS commands are pretty dangerous. Digital Research has made their operating system safer than the other guys, but any operating system is inherently dangerous. Anything that gives you the power to format disks and erase files has the potential for accidents!
Typing "format" instead of "format A:" can destroy the data on your hard disk. Sure, Digital Research has built in unformatting to help you recover from such an accident, but do you really want to even take the chance of a mishap? Additionally, "erase *.DOC" instead of "erase *.BAK" can erase all your documents. Without Delwatch active, you'll need the Norton Utilities (or a similar program) and some luck to get your files back. Batch files can form a strong line of defense against the indiscriminate use of these powerful commands.
Remember: Batch files make dangerous commands safer.
You can prevent accidental formatting of a hard disk by first renaming FORMAT.COM to XYZ.COM and then creating FORMAT.BAT with the single command @XYZ A:. The @ sign will be examined later, but I'll reveal now that it keeps the XYZ from showing on the screen. If you saw the name, you might be tempted to bypass the batch file. We'll find out later how to construct more complex batch files to help prevent erasing our .DOC files.
Remember: Batch files are the easiest programming language to learn!
When most computer users think of programming, they think of spending months to learn languages such as Pascal or C. Like programs created with Pascal or C, DR DOS batch files are programs. Unlike other programming languages, however, batch file commands are made up of mostly DR DOS commands, most of which are already familiar to most of you. Therefore, you already know most of the commands you need to write a batch program. Batch files have only a handful of special commands; the vast majority of batch file commands are the regular DR DOS commands you use every day. If you know how to use DR DOS, you know most of what you need to know in order to write effective batch files.
When you learn to program in most computer languages, you must learn a great deal before you're able to write even the simplest program. This isn't true with batch files, though. As you've soon see, you can write some very powerful batch files with standard DR DOS commands and just a few special batch commands.
Although batch files are easy to write, this ease comes at a price: batch files are limited in what they can do. One of your most common thoughts as you study this book will be "If only..." You'll see many situations where you could really automate things if only batch files had specific commands. That is the real drawback to batch files: They're easy to learn and easy to write, but they always seem to stop just short of having enough power.
One way to overcome many of the limitations of DR DOS batch files is with the many batch utilities available in the public domain as shareware and commercial programs. Many of these are described in my book MS DOS Batch File Utilities, also available from Windcrest. Additionally, this book includes a copy of Batch Commander, a special batch file utility kit written especially for my batch file books by Doug Amaral, chief programmer at hyperkinetix.
The information for this book was taken from the last draft I submitted to the publisher. Since the publisher performed minor editing, the version you purchase in the store will be slightly different.
Order One Of These Books
. Amazon.com is an on-line bookstore with a very large list of books, including all of mine that are still in print. This link will take you to Amazon.com using a special page they set up to showcase all my books. They are the easiest way I know of to order my books on-line. Of course, you can use this page to order any of the books they carry.
© 2002 by Ronny Richardson, All Rights Reserved