Just What are Batch Files Anyway?

In their simplest form, batch files are just files that COMMAND.COM interprets as a sequence of commands, as if they had been entered from the command prompt. A trivial example is a batch file to invoke a program and then clear the screen when it finishes:


 edit
 cls
Another simple example creates a directory and changes to it:

 md test
 cd test
However, that isn't really useful, since the program would be needed only once, and it is less effort to simply issue the commands from the prompt. Batch language has extension to the commands that can be issued from the prompt to allow replaceable parameters to be passed on the command line when the batch program is invoked. Adding a single replaceable parameter (command line arguments are referenced by '%' followed by a number) to the above makes it quite useful:

 md %1
 cd %1
If this batch file were named ND.BAT, then the command

 ND test
would create the subdirectory \TEST in the default directory and change to it automatically. The first example can also be improved by adding "%1" after the EDIT command, so that you can specify the file to be edited in the invocation command.

Even single line commands can benefit from being encapsulated in a batch file, especially where the command is lengthy and only one or two elements vary from use to use, as in this program (UNZIP.BAT) that I have used for years:


 pkunzip -d %1
which never forgets to use the -d switch for PKUNZIP (to properly expand a ZIP file containing subdirectories). Another useful example is QBASRUN.BAT, which launches QBASIC and causes it to automatically execute the program passed as the command argument:

 qbasic /run%1

Obviously, there is more to batch files than indicated here, but this is pretty much the original idea behind them. There are many other features and enhancements, but these will be addressed in other essays.

  ** Copyright 1995, Ted Davis - all rights reserved ** 

Input and feedback from readers are welcome. NOTE: the subject of the message must contain the word "batch" for the message to get past the spam filter.

Back to the Table of Contents page

Back to my personal links page - back to my home page