Introduction to Batch Files in DOS
Module Q10cd

Audience and Objectives |
About
this document...
Contents
Wherever you see this separator line in the document,
clicking on it will return you to the Contents.

What's a Batch File, and What's it For?
Batch files give you a way of creating your own commands in DOS, by typing
a series of commands into a file, and having DOS run all the commands together.
Here are some of the things you can do with a batch file:
-
Customize your system with programs that run at startup time
-
Copy files from multiple directories to create backups
-
Run a series of programs with one command
-
Simplify complex commands by putting them in a file with a simple name
-
Any repetitive series of commands that need to be run in several directories
Batch files provide a way to do many things that can't be done in a graphical
user interface (GUI) such as Windows. They can be a relatively simple series
of commands, or you can put in simple conditional "if" statements that
allow the batch file to choose between several options, like a program.
You can even have it do loops!
Since batch files are simple ASCII text files, you don't need any special
program to create them. You can use any text editor or word processor.
The following section shows how...

Creating, Editing and Running a Simple Batch File
Here's an illustration of creating a batch file. We'll make one that displays
a directory in alphabetical order, pausing when the screen gets full.
To start, we need to run an editor. Simple editors (like DOS Edit and
Windows Notepad) are usually easier and quicker than full-blown word processors
(such as MS-Word or Word Perfect) because they load faster and they automatically
save in ASCII format. The big word processors save, by default, in their
own special format (which is really a sort of database!). In the SaveAs
dialog box, you can specify saving as text or ASCII.
When our editor is running, we'll type in the directory command with
the proper option switches:
The switch /o tells the directory command to put the listing in
order; the simplest case, shown here, assumes you want them in order by
file name. The switch /p tells dir to pause when the screen is filled.
For more information about the dir command, at a DOS prompt type
dir
/?
Let's save the file with a simple name: d.bat
All batch files have the extension .bat which is necessary
for the file to work in DOS.
(Remember: if we use a word processor, we must make sure the
file is saved as text.)
Running a batch file is simple: just type its name (the extension isn't
needed) at a DOS prompt, and DOS will run it by executing all the commands
in it. In this case, we should see something like this:
C:\>d
C:\>dir *.* /o /p
Volume in drive C is L_KRIEG_15G
Volume Serial Number
is 33F3-0B09
Directory of C:\
AAAMNG3
<DIR> 02-25-98 3:06p
Aaamng3
ACROREAD
<DIR> 08-21-97 6:28a
ACROREAD
ATLAS4
<DIR> 08-30-97 11:27a Atlas4
ATLAS5
<DIR> 11-13-97 10:31p Atlas5
BACKUP
<DIR> 07-30-97 10:55a BACKUP
CSERVE
<DIR> 09-28-97 5:43a
CSERVE
DELL
<DIR> 07-30-97 10:52a DELL
DELLMO~1
<DIR> 08-08-97 9:41a
Dell Mobile Solutions Pack
DMI
<DIR> 07-30-97 10:54a DMI
DOS
<DIR> 07-30-97 10:52a DOS
EXAM
<DIR> 09-09-97 7:13a
EXAM
KURZWEIL
<DIR> 03-05-98 6:56a
KURZWEIL
MONEY
<DIR> 10-17-97 10:44p MONEY
MYDOCU~1
<DIR> 08-13-97 10:22a My Documents
NOVELL
<DIR> 08-08-97 2:54p
NOVELL
PCW
<DIR> 09-08-97 7:00a
PCW
POWERT~1
<DIR> 08-18-97 5:52p
PowerToys
PROGRA~1
<DIR> 07-30-97 10:50a Program
Files
PSFONTS
<DIR> 08-21-97 6:38a
psfonts
Press any key to continue .
. .
|
On the first line, we typed d (the name of
the batch file without its extension) and pressed the <Enter> key. DOS
found the batch file and starting running the commands in it. Normally,
the commands are shown on the screen (the second line here), but that can
be turned off, as we will see shortly.
Note: in order to run a batch file, DOS has to be able to find it. It
can do this if:
-
The batch file is in the current directory. (In this example, the batch
file was in the root directory, and as you can see from the prompt, that's
what the default directory was too.)
-
The batch file is in the "path". The path is a DOS listing of directories
to look for executable programs, and can be set with the path
command. For example:
path c:\; c:\dos; c:\windows
You now know the basics of batch files: how to create them and how
to run them. In a batch file, you can put any DOS command, so if you are
already familiar with DOS, you can create and run batch files as much as
you like!
But there are things you can do in batch files that you can do nowhere
else in DOS. Some of these are features that make batch files practical
for a wide range of tasks that would otherwise be impractical. The rest
of this module and the "Conditional Batch Commands in DOS" Module (Q12cd)
introduce you to some of these batch commands.

Batch File Documentation
Batch files can be created very simply and easily, and can live on
your computer indefinitely. There usually comes a time when you forget
what a particular batch file is for, you look at it with an editor, and
start puzzling out what it is...
This puzzlement can be avoided by including comments explaining what's
going on. Like any computer programming situation, it takes a certain amount
of self-discipline to actually put comments in; but once you get in the
habit, you'll find it's easy and it really pays off down the road! Also,
professional batch files (those created by technicians for use by other
people) are usually required to have documentation within them.
How to Add Comments
There are two ways to add comments to batch files without getting an error
message when you run the program:
-
The rem statement: Put the word rem at
the beginning of a line, and the line will be ignored when you execute
the batch file. The line can then be used to explain the batch file or
some part of it. ("Rem" is short for remark.)
-
The : colon: Lines beginning with the color : can be used
as documentation, though the colon is also a label marker. (Labels, which
are used in conditional batch statements, are points to which you can pass
control within a batch file.)
What Comments to Add
There are a couple of items every batch file should have:
-
What the file is for, and how to use it
-
Author's name or initials, and the date created and modified.
In addition, some batch files may benefit from information about some of
their internal details. For example,
-
If the batch file uses parameters (which we'll
discuss soon), these should be explained.
-
If conditional statements are used (and we'll
touch on them, too), the condition tested should be explained.
-
If the file is long and complicated, with several sections, the purpose
of each section should be explained.
-
In general, it's good to explain anything you can to make the process easier
to understand.
An example of documentation
OK, let's add comments to our file, d.bat. First, let's use rem
to illustrate the technique:
rem --- D.BAT: list the current directory
rem in order by
file name,
rem pausing when
the screen gets full.
rem (LJK 3/23/98)
dir *.* /o /p
|
Now using the colon to mark comments:
: --- D.BAT: list the current directory
: in order by
file name,
: pausing when
the screen gets full.
: (LJK 3/23/98)
dir *.* /o /p
|
The output of both these examples is example the same as that of the first
example, where we had no documentation at all.

Echoing and Logging
In a simple batch file, having the DOS commands show on the screen is usually
not a problem. As the process gets more complex, the output gets more confusing
to the user, and most batch file writers prefer to spare the user the details.
But sometimes the details need to be recorded for later analysis. These
options can be handled in batch files by using the echo command
and output redirection.
Echo
The echo command is available to allow you to control what
the output will look like. It can be used to:
-
Prevent the DOS commands from being shown (echoed) to the screen. Example:
echo off
-
Make sure the DOS commands are being being shown on the screen.
Example:
echo on
-
Send messages to the screen. Example:
echo All done. Have a good day!
Let's add some output control to our file d.bat.
: --- D.BAT: list the current directory
: in order by
file name,
: pausing when
the screen gets full.
: (LJK 3/23/98)
echo off
dir *.* /o /p
echo All done. Have a good day! |
Here is the output from running this batch file on the a: drive:
| A:\>d
A:\>echo off
Volume in drive A is
L KRIEG
Volume Serial Number
is 136C-17EF
Directory of A:\
97F_FULL TXT
34,246 08-28-97 8:48p 97F_FULL.TXT
BIGARR DAT
2,624 11-20-97 8:57a BIGARR.DAT
D
BAT 199
03-23-98 8:29a d.bat
MP6
DAT 270
11-23-97 4:55p mp6.dat
MSS_STU DOC
27,648 03-12-98 4:04p MSS_STU.DOC
SURVEY1 XLS
109,056 08-18-97 12:45p SURVEY1.XLS
WCCNDS~1 DOC
82,944 08-20-97 5:46p WCCNDSRights.doc
7 file(s) 256,987 bytes
0 dir(s) 470,016 bytes
free
All done. Have a good day!
A:\>
|
Notice the message at the end of the listing.
Notice also that the directory command itself doesn't show on the screen,
but the command echo off does show. If you want to hide
the echo off command, put the symbol @ in front of it, like this:
@echo off
Redirecting Output to Keep a Record
If you want information to go to a file rather than to the screen, DOS
allows you to redirect output using the symbol >
If you want to collect information from several commands into one file,
you need to double the symbol >>
otherwise, DOS deletes the file and recreates it each time.
You can do this either at the DOS prompt or in a batch file. Let's modify
the file to send output to a log file:
: --- D.BAT: list the current directory
: in order by
file name,
: pausing when
the screen gets full.
: (LJK 3/23/98)
@echo off
dir *.* /o /p > d.log
echo All done. Have a good day! >> d.log |
Let's run the modified file and look at the output:
A:\>d
A:\>type d.log
Volume in drive A is
L KRIEG
Volume Serial Number
is 136C-17EF
Directory of A:\
97F_FULL TXT
34,246 08-28-97 8:48p 97F_FULL.TXT
BIGARR DAT
2,624 11-20-97 8:57a BIGARR.DAT
D
BAT 226
03-23-98 8:55a d.bat
D
LOG
0 03-23-98 8:55a d.log
MP6
DAT 270
11-23-97 4:55p mp6.dat
MSS_STU DOC
27,648 03-12-98 4:04p MSS_STU.DOC
SURVEY1 XLS
109,056 08-18-97 12:45p SURVEY1.XLS
WCCNDS~1 DOC
82,944 08-20-97 5:46p WCCNDSRights.doc
8 file(s) 257,014 bytes
0 dir(s) 468,992 bytes
free
All done. Have a good day!
|
This time, the d command didn't show any output on the
screen. We were just returned to the DOS prompt. Using the type
command,
we can look at the file d.log to see our results. The output looks
pretty much like any directory, but now it's in a file and can be displayed
or edited in a text editor or word processor.

Adding Parameters to a Batch File
One of the things that makes computers useful is their adaptability: you
can have them do many related tasks. You can get batch files to do several
related tasks by using parameters. A parameter is a piece
of information you give on the command line when running the batch file,
which can be used to modify the action in some way or other.
Let's take our directory batch file as an example. As we initially constructed
it, it only does a listing of the current directory on the current disk.
Also, we've sent the output to a log file. Either one of these can be modified
if we use parameters. Here's how it works...
The Command Line with Parameters
Every word you add to the command line is counted as a parameter. Within
the batch file, each word can be referred to by a percent sign % and the
number of the word. We could set our file up to take one or two words representing
a directory to list and a file or device to send the output to. Our command
line could look like any of these:
d c:\temp prn
d a:\ a:floppy.lst
d c:\windows
The letter d in each example is the name of the batch file.
In the first example, c:\temp is the directory we want a listing of,
and prn (the device name of the printer) is where we want it sent.
In the second example, the a: diskette drive is what we want listed,
and the file floppy.lst on the a: drive is where we want the listing sent.
In the third example, we're asking for a listing of the Windows directory
on c:, but we're not giving it an output destination. With no destination
listed, the batch file will use the default output device: the screen.
Using Parameters in Batch Commands
Now: how do we modify the batch file to use these parameters? Here's the
file, modified to do what we want:
: --- D.BAT: list the current directory
: in order by
file name,
: pausing when
the screen gets full.
: (LJK 3/23/98)
: %1 is the directory
to list
: %2 is the output
destination
@echo off
echo Listing %1 to file/device %2
dir %1*.* /o /p > %2
echo All done. Have a good day! |
Notice that we've added a comment statement briefly explaining what
each parameter is supposed to do. Also, so the user will feel more confident
of what's going on, we've added a screen echo saying what we're doing.
The "All done" echo is directed to screen as well. (In the previous example,
we appended it to the log file, remember?)
We now have a good, flexible batch file. To see what it can do, type
it in and try it with several combinations of parameters!

Batch Conditional Statements
"Conditional statements" allow batch files to test a number of conditions
and transfer control to other statements. These are discussed in detail
in module Q12cd,
but here's an idea of what they can do...
Conditional statements can:
-
Test for the existence of files
-
Test to see if a parameter has a certain value or not
-
Test the "return value" of a program
Conditionals are used with labels and the goto statement to direct the
operation of the batch file. Link to "Conditional
Batch Commands in DOS" for a continuation of this discussion.
For exercises to practice creating simple batch files, see module Q11hd,
"Creating
Simple Batch Files in DOS".
Audience:
This module is directed at people with an understanding of DOS commands,
who would like to learn how to create simple batch files. For the basics
of DOS, please refer to module Q02cd.
Objectives
When you successfully complete this lesson, you will be able to...
-
Create a batch file using a simple editor
-
Save the batch file with the proper extension
-
Discuss the use of command-line parameters
-
Use more than one command-line parameter in a batch file
-
Document the file with either REM, ECHO, or :
-
Turn echo on and off
About this document...
Module Q10cd: Introduction to Batch Files in DOS
This document is part of a modular instruction series in Computer Information
Systems. For more information, see the overview
or the list of modules in this series, Q-Modules:
Operating Systems. This document has been used in the following classes:
CIS
290 Microcomputer System Support.
-
Author:
-
Laurence
J. Krieg
-
Institution:
- Department
of Computer Information Systems, Washtenaw
Community College
-
History:
-
Original: 23 Mar 1998; links corrected 20 Dec 1999
-
Copyright:
-
Copyright © 1998, Laurence J. Krieg.
Instructors: You may point to this file in your Web-based materials.
Students: you may make a copy for your personal use.
All other uses: contact the author, Laurence
J. Krieg for permission.