How to Create a DOS Batch File
Previous Topic  Next Topic 

Command Line Invocation

Running programs in a DOS Command Window straight from the command line has the limitation that you can only issue one command - you type the name of the program, then any control parameters and options, and then terminate the command line by pressing the Enter key at the end. This runs the program with the options you specified.

For example, NetMailBot is invoked directly on the command line ("C:\>") according to the following:

NetMailBot -to {To} -from {From} -subject {Subject} -server {Name of your SMTP Server} [Optional Parameters] (Enter)


Note that there is no newline (press of the Enter key) until the end; if the above example takes two physical lines on your screen, it's due to word wrap.


If you just want to use NetMailBot in simple situations, this might be acceptable. But if you use a lot of parameters and options, you don't want to have to type them all in each time you want to run NetMailBot. Also, if you have more complex situations, when the context of NetMailBot's use depends on other factors and decisions that have to be made "on the fly", then you need a "little program" that calls NetMailBot within the context of its own execution.


Thus, you need a batch file!

What is a batch file?

Batch files are special files, often called scripts, that allow you to run programs from the DOS Command Window. Batch files are useful for storing sets of commands that are always executed together, in sequence, because you can simply enter the name of the batch file as the "command" instead of entering each command individually. 

In DOS, batch files end with a .BAT extension. The process of creating them is often called DOS batch scripting.

A Simple Example

Suppose the following text is saved in a file called "mybat.bat":


REM This is an example .BAT file output...



REM Nothing fancy...



This batch file contains two comments (or remarks, indicated by "REM") that just provide documentation for either the author or the user of the file. The first one is "echoed" to the screen because it is embedded inside two @ECHO commands that first turn on echoing and then turn it off. Everything between the @ECHO commands is displayed on the screen during the script's execution. After the second comment, there is a simple DOS "dir" command which, as usual, lists the contents of the current directory.


Note that the individual commands or comments are separated by newlines, that is, pressings of the Enter key.


Running the batch file executes the commands in "mybat.bat" sequentially.


Invoking the Batch File


A batch file can be run in two ways:



For our example above, typing "mybat" or "mybat.bat" at the DOS command line yields the following output:



Double-clicking the batch file icon (see below) will open up a DOS Command Window, invoke NetMailBot within it according to the batch file, and then close the window when everything is done. Depending on how short your script is, if you blink, you might miss it!


A batch file icon:


See the More Information on DOS Batch Scripting topic for more information.