Episode: 3700
Title: HPR3700: Introduction to Batch Files
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr3700/hpr3700.mp3
Transcribed: 2025-10-25 04:17:20

---

This is Hacker Public Radio Episode 3,700 for Friday the 7th of October 2022.
Today's show is entitled Introduction to Batch Files.
It is part of the series DOS.
It is hosted by AOKA, and is about 15 minutes long.
It carries a clean flag.
This summary is More on DOS.
This time is Introduction to Batch Files.
Hello, this is AOKA, welcoming you to Hacker Public Radio.
And another exciting episode in our ongoing DOS series.
And what I want to do today is begin taking a look at Batch Files.
It's an important concept.
Now a Batch file is a file that contains a number of DOS commands, each of which you could
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 the DOS edit, which we had
looked at previously in preparation for all of this stuff, or you could do it in, if you
were in Windows with notepad.
When you have created the Batch file, you save it with a file name, and give it the extension
.bat.
Now, 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 configsys file.
Examples of these include buffers, country, device, and so on.
Now when you create a Batch file, you are beginning to write a program, basically, DOS, Batch
files, may not have the power of a structured programming language, but they can be very
handy for 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 from 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, place the REM at the beginning of the line you
want to disable.
When you want to reenable that command, just open the file for editing and remove the REM,
and the command will resume functioning.
This technique is sometimes referred to as remarking out or commenting out a command.
Now from this description, you may think this sounds familiar.
In the Unix or Linux world, this is simpler to things like a bash script, which is very
much the same kind of thing, a series of commands that you can execute, and they will execute
in order.
Now our DOS Batch files is good as bash scripts.
Well they don't have all of the power that a bash script has, and of course, bash is only
one of a number of shells that you could be operating.
It's the one that I use.
It's the most common one.
So I'm not really qualified to comment on Z-shell or C-shell or what have you because I don't
use any of those.
I'm bash is the one I'm used to.
But there's nothing new under the sun in other words.
Now Batch files can save time.
Many years ago I was at a technology conference for college professors.
I was a college professor at the time, and faculty development officer at the college
I was at.
At the end of the conference we needed to quickly make about 40 floppy disks, each with an
identical set of about 15 vials.
There are various ways of doing this, such as using disk copy or copy commands, but I
wanted to do this as quickly and efficiently as possible.
So I sat down with 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.
And that's one of the things you can do is you can type right at the console, and the
command I used was C colon, backslash, right at the prompt, the C colon prompt, backslash
copy, con, copy, space, con, space, one, dot, BAT, space, copy, space, C colon, backslash,
temp, backslash, star, dot, star, space, a colon, space, control, Z.
So what's this all about, copy, space, con, that is saying I'm about to type something
into this computer, and I want you to copy whatever I type into a file, then one dot
BAT, that's the name of the file, right, then another copy.
Now this is a command that's going to be in the file, and what is the command copy, space,
C colon, backslash, temp, backslash, star, dot, star, that's saying copy everything in the
temp directory, where do we copy it, space, a colon, copy it to the a drive, control,
Z, I'm done, end copying into this file, and then back comes a response from the computer,
it says one file, parentheses, S, close parentheses, copied, all right, so we copy from the
console, we store it in a file called one dot BAT, the console in this case just means
the keyboard, it's just taking whatever I type and entering into a file, and the second
line is the single command in my batch file, it copies all of the files in the directory,
C colon, backslash, temp, to the floppy disk in the a drive, third line is holding the control
key while typing 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 the response from the operating system
saying okay boss, we copied your file, now I could then view this file in several ways,
I could use the type command, the type command will cause DOS to open a file and display
the contents on the screen, it's typing it out for you, I could open it in edit, and if
I did I would see the single line, copy, space, C colon, backslash, temp, backslash, star dot
star, space, a colon, now 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, now once I
created this file all I had to do was feed in a floppy, press the one 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 one 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, now another great use for batch files 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 space, C colon, backslash, temp, backslash,
C colon, backslash, temp, backslash, star dot star, space, DEL space, C colon, backslash,
windows, backslash, temp, backslash, star dot star, with these two commands you can clean
out two directories that might otherwise gradually accumulate a lot of temporary files.
Create the batch file, stick a shortcut to it in your start up folder, and those directories
will be cleaned out automatically every time you boot windows. Now if you want to be a
little more conservative and you worry that you might delete something important, what
you could do is alter your commands like this, so the first command would be DEL space, C colon,
backslash, temp, backslash, star dot TMP, and then for the other one DEL space, C colon, backslash,
windows, backslash, temp, backslash, star dot TMP. Now you will only delete files with the TMP
extension, and by definition those files are safe to delete. Now batch files from multiple
commands, and to really tap the power of batch files, you need to use multiple commands.
Now on my website, and there's a link in the show notes to the page, I take an actual example
from the windows 95 installation CT ROM, and start picking it apart. It's complicated,
and if I try and just recite all of this stuff, it's going to be very boring audio.
So let me just give you a little overview. It starts off with a command to turn off echo,
so it's not going to throw everything up on the screen. Then there's comments to say this is what the
section of the file is doing, it's looking for the name of your windows directory. For most people it would have been
C Windows, C colon, backslash, windows, but mine at the time was C colon, backslash,
win 95, because I used to use dual boot machines a lot. In fact I was kind of famous for having
one machine that had I think six different operating systems I could boot into at the boot time.
Then it's looking for the name of the directory, if it does not find a name, it'll jump down to a section called no
window, no window directory. If it does find a name, it'll keep going with the commands in order.
Then there's some more comments about the next section, what it expects to find. If it doesn't find it,
it'll jump down to the no file section. If it does find it, it'll keep going through the commands in order.
Then there are some commands to rename the file and so on. Finally there's a thing at the end that says,
we're done, this ends the batch file and it stops running. This is an example of a moderately complex
batch file that uses excellent technique in this documentation. All of the remarks I think are a wonderful
thing. You happen to have a Windows 95 installation CD or what have you lying around, you know,
check it out for yourself. Each section has an REM section of the batch file has an REM section that
explains what's 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's trying to follow what the batch file does. So you know, you put in
to do that, you would do REM hit the enter key, REM again and now write your remark hit the enter key
and then write REM again hit the enter key. So you've got blank lines above and below. It's a great
technique. I really love it. I say you can see it on my website or take a look at it if you have the
media around yourself, but it is wonderful and I'd encourage everyone to take a look at it. So this is a
hookah for Hacker Public Radio signing off and is always encouraging you to support free software. Bye-bye!
You have been listening to Hacker Public Radio at Hacker Public Radio does work. Today's show was
contributed by a HBR listener like yourself. If you ever thought of recording podcasts, click on our
contribute link to find out how easy it really is. Hosting for HBR has been kindly provided
by an honesthost.com, the internet archive and our sync.net. On the Sadois stages,
today's show is released under Creative Commons, Attribution 4.0 International License.
