Batch Files

Sometimes you will want to execute the same sequence of commands over and over again. This is easy to do; just create a file containing those commands (one command per line) and give it a name with a .BAT extension (e.g. DO_IT.BAT). A file like this is known as a batch file. Once you've done this you can just type DO_IT at the command prompt, and (assuming that DO_IT.BAT is the first file that the command interpreter finds when it's looking for the command, as described above) the command interpreter will read this file and execute each line as a separate command. For example, assume that DO_IT.BAT contained the following lines:

Batch Files are such simple timesavers that one can write and edit oneself. To learn to write batch files, you can download any of our demos, all of which have simple batch files to start them, and copy the code for them. It's pretty easy. Use EDIT to create batch files you need to accomplish things you do more than once or twice, or to develop series of commands you want to execute repeatedly. Batch files don't have to be big -- I have plenty of one-line batch files which I give short names to and which might only execute one thing. Batch files can call other batch files or jump to other batch files, which increases their usefulness substantially. Batch files are time savers and they help you to do things quickly and efficiently and the same way every time. In order to save keystrokes, I often rename the few programs I use regularly, like BRIEF.EXE which is renamed b.exe and placed on my virtual drive for virtually instant access. (Note: as of 1997, I now use BOXER by David R. Hamel, which is started with the initials TKO. Also, like many people, I have finally made the complete switch to Windows 95 as the base operating system on the computer, and no longer make use of a virtual drive for my individual DOS sessions (since they would take too long to set up each time, and hard drives are much faster.)


Typing DO_IT would clear the screen and the display the message "HELLO, WORLD". By default, each line of the file is displayed on the screen ("echoed") before it's executed. This is useful for debugging but it ruins the effect in the batch file above. You can disable the echoing by prefixing the command with "@":


Another method is to disable echoing at the start of the batch file with the command "ECHO OFF" (which needs to be prefixed by "@", otherwise it will be echoed to the screen):


Batch files can be quite sophisticated. For example, you can get at the command line parameters by referring to them as %1, %2, %3 and so on up to %9. The following batch file displays the second command line parameter followed by the first one:

ECHO %2 %1

If this is saved as DO_IT.BAT, typing


will display the message


There are also commands which give you the basics of a simple programming language: IF statements to choose between alternative courses of action, FOR loops to repeat a sequence of actions, tests to determine if a command executed successfully, and so on.

One nasty trap to watch out for is when you try to execute a batch file from inside another batch file. Imagine you've got this in DO_IT.BAT:

ECHO Finished!

The ECHO command on the second line won't be executed; if you refer to one batch file inside the other, the effect is a GOTO rather than a CALL (i.e. you go to the inner batch file but you don't remember where you came from, so at the end of the inner batch file you end up back at the command prompt). The solution is to use the internal CALL command instead:

ECHO Finished!

This will work as expected; it'll execute DO_OTHER.BAT and then display the message "Finished!".

The file C:\AUTOEXEC.BAT is a special one, because when the system is first started the command interpreter will automatically execute this file as a batch file (hence the name). This is where to put all your system startup commands; for example, setting the path, loading your keyboard and mouse driver, running a virus checker, or whatever. Whenever you install any new DOS software, you'll need to check whether you need to update this file (and if you do, you'll either need to restart the system or type C:\AUTOEXEC.BAT to re-execute it before any of the changes will take effect). You'll often need to update your path to include the directory where your new software was installed. Some software will do this automatically, and amazingly often it will get it wrong or do it in such a way that something else no longer works. What I always do is to have a separate file (C:\BOOT\AUTOEXEC.BAT) where all the "real" work is done, and then I have a one-line C:\AUTOEXEC.BAT file which says this:


Remember, C:\BOOT\AUTOEXEC.BAT will never go back to C:\AUTOEXEC.BAT when it's finished (I deliberately didn't use a CALL command). This means that anything that an auto-installation utility adds to the end of C:\AUTOEXEC.BAT will be ignored, and I can inspect it and decide whether it's made a sensible change or not. If so, I can then update my real AUTOEXEC.BAT file to include the changes.