DOS Lesson 15: Introduction to Batch Files

A batch file is a file that contains
a number of DOS commands, each of which could be run individually from the command
prompt. By putting them into a batch file, they can be run as a group by simply
running the batch file. Note that the commands execute in the order they appear
in the batch file, and that anything that causes a command to halt will also
halt the batch file.

You create a batch file by using an ASCII text editor, such as
DOS EDIT, or Windows Notepad. When you have created the batch file, you save
it with a file name, and give it the extension *.bat. Note that you must not
use a name that is the same as any DOS commands or any other program or utility
you are likely to run. If you use a DOS command name, trying to run your batch
file will not work because the DOS command will execute first. If your name
matches some other program or utility, you may never be able to run that program
again because your batch file will run before the program runs. So pick something
that is not likely to match any command name or program file name.

Virtually all internal and external commands can be used in a
batch file. The few exceptions are the commands that are intended only for configuration
which are used in the CONFIG.SYS file. Examples of these include BUFFERS, COUNTRY,
DEVICE, etc.

When you create a batch file, you are beginning to write a program,
essentially. DOS batch files may not have the power of a structured programming
language, but they can be very handy for handling quick tasks. Because this
is a form of programming, let us begin with learning some good habits. The number
one good habit for any programmer to learn is to put comments in the program
that explain what the program is doing. This is a very good thing to do, but
you need to be careful not to fool the operating system into trying to “execute”
your comments. The way to avoid this is to place REM (short for Remark) at the
beginning of a comment line. The OS will then ignore that line entirely when
it executes the program, but anyone who looks at the “source code”
in the batch file can read your comments and understand what it is doing. This
is also a way to temporarily disable a command without deleting it. Just open
your batch file for editing, and place the REM at the beginning of the line
you want to disable. When you want to re-enable that command, just open the
file for editing and remove the REM and the command will resume functioning.
This technique is sometimes referred to “remarking out” or “commenting
out” a command.

Batch files to save time

About 7 or 8 years ago, I was at a technology conference for college
professors, and at the end of the conference we needed to quickly make about
40 floppy disks, each with an identical set of about 15 files. There are various
ways of doing this, such as using DISKCOPY or COPY commands, but I wanted to
do this as quickly and efficiently as possible. So I sat down at the computer,
which was running DOS, and quickly copied the 15 files into a temporary directory
on the hard drive. I could have then opened EDIT, but to be even faster I entered
it directly from the console as follows:

C:>\copy con 1.bat

copy c:\temp\*.* a:

^Z

1
file(s) copied

Let’s go through this to see what I did. The first line says to
“copy” from the “console” and store it in a file called
“1.bat”. The console in this case means the keyboard, sort of. It
is just taking whatever I type and entering it into a file.

The second line is the single command in my batch file. It copies
all of the files in the directory C:\Temp to the floppy disk in the A: drive.

The third line is holding the Control key while typing “Z”,
called a Control-Z. This is the End-of-file marker, and tells the OS I am through
entering text into this file.

The fourth line is not anything I typed. It is the OS responding
to me that it had copied my file.

I could then view this file, either using the TYPE command or
by opening it in EDIT, and I would see the single line “copy c:\temp\*.*
a:”. If I opened it in EDIT I could then add more commands or whatever
I wanted to do with it. But in this case I didn’t want to do anything else.

Once I had created this file, all I had to do was feed in a floppy,
hit the “1″ key, then the ENTER key, and the batch file would copy
everything. As soon as one disk had received its contents, pop it out, put in
a fresh disk, hit the “1″ key, then ENTER, etc. I had the 40 floppy
disks done in not much more than 10 minutes. In this case, I used the batch
file to automate a repetitive process and save me some keystrokes.

Batch files to automate tasks

Another use for a batch file is to clear out your temporary directories.
This trick works great in Windows, which uses batch files just like DOS. Create
a batch file like this:

del C:\Temp\*.*

del C:\Windows\Temp\*.*

With these two commands, you can clean out two directories that
might otherwise gradually accumulate a lot of temporary files. Create the batch
file, and stick a shortcut to it in your Startup folder, and these directories
will be cleaned out automatically every time you boot Windows. If you want to
be a little more conservative, and you worry that you might delete something
important, alter your commands like this:

del C:\Temp\*.tmp

del C:\Windows\Temp\*.tmp

Now you will only delete files with the *.tmp extension, and by
definition those files are safe to delete.

Batch files for multiple commands

To really tap the power of batch files, you need to use multiple
commands. Here is an example from the Windows 95 installation CD-ROM:

****************************************************************

@echo off
rem
rem Get the name of the WINDOWS directory
rem
if “%WINDIR%”==”” goto NOWINDIR
rem
rem See if the netibmcc.inf file exists
rem
if NOT EXIST %WINDIR%\INF\NETIBMCC.INF goto NOFILE
rem
rem Rename the file
rem
if EXIST %WINDIR%\INF\NETIBMCC.ORG del %WINDIR%\INF\NETIBMCC.ORG
ren %WINDIR%\INF\NETIBMCC.INF NETIBMCC.ORG
echo %WINDIR%\INF\NETIBMCC.INF renamed to NETIBMCC.ORG
goto end
:NOFILE
echo fixibmcc: No NETIBMCC.INF file found in %WINDIR%\INF
goto end
:NOWINDIR
echo fixibmcc: No WINDIR environment variable was found
goto end
:end

**********************************************************************

This file, by the way, is in \drivers\netlan\ibmtkpcm\, and is
called fixwin95.bat. Let’s see what it is doing.

Line 1 turns off the echo. Echo is what displays the commands
on the screen as they are executed, so turning it off means you will not see
what is going on as this file executes.

Lines 2-4 are comments to tell you what this section of the file
is doing. In this case it is looking for the name of your Windows directory.
For most people this would be C:\Windows, but you can change it. Mine is C:\Win95,
for instance, because I frequently use dual boot machines.

Line 5 is the command that is looking for the name of the directory.
If it does not find a name, it will jump down to the section called NOWINDIR.
If it does find a name, it will keep going with the commands in order.

Lines 6-8 are also comments that explain that the next section
will look for a file named netibmcc.inf, which it expects to find in your Windows
directory, in the INF folder.

Line 9: If it does not find it, it will jump down to the NOFILE
section. If it does find it, it will keep going through the commands in order.

Lines 10-12, comments that it will now rename the file.

Line 13 begins a slightly more complex combination. It looks for
a file named NETIBMCC.ORG, and if it finds such a file, it deletes it.

Line 14 takes the file NETIBMCC.INF, and renames it to NETIBMCC.ORG.

Line 15 uses the echo command to display a message on the screen.

Line 16, it is then done, and jumps to the end.

Line 17 begins the NOFILE section. The batch file would have jumped
here if the file NETIBMCC.INF had not been found where the batch file expected
to find it. In this case, it uses the echo command to display an appropriate
error message on the screen, then jumps to the end.

Line 20 begins the NOWINDIR section. The batch file would have
jumped here if the Windows directory had not been found. In this case, it uses
the echo command to display an appropriate error message on the screen, then
jumps to the end.

Line 23: This is the end of the batch file, and it stops running
here.

This is an example of a moderately complex batch file that uses
excellent technique in its documentation. Note how each section has a REM section
that explains what is going on. Also, note how the writer used extra blank REM
lines above and below each remark to set it off from the rest of the batch file.
These are not needed in any sense, but they make it more readable for a person
who is trying to follow what the batch file does.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>