To Temple College Class  logo R. Craig Collins > ITSC 1305 > Batch Files

Batch files © R. Craig Collins, 2005/6

A batch file is a plain text document that includes a series of DOS commands, and has been given the extension .bat

A batch file is used to automate some process you do a lot; most DOS computer users prefer the prompt to include the path and the greater than sign, and most DOS computer users prefer the PATH to point to C:\DOS... so rather than type these two commands in every time they start their computer, most users created a batch file to automate the process.

This special batch became so popular that Microsoft set up DOS to automatically look for this one file, and run it at start up. The file is called autoexec.bat, and the contents are (as described above)
prompt $p$g
path C:\DOS

But you can create a batch file to automate any process that you do a lot.

There are two ways to create a text file
1) use copy con filename.bat
    type in the DOS commands (one per line)
    then end the process with [Ctrl]+[z]

or

2) use EDIT filename.bat
    type in the DOS commands (one per line)
    then end the process with [Alt]+[f]+[x]

To facilitate batch files, you may use three special batch file commands
1) rem is a remark... this line is ignored by DOS
2) pause interupts, or pauses the batch file until the user presses a key
3) echo controls what is or is not displayed... if the file starts with @ECHO OFF, nothing is displayed unless specified in the batch file, using something like ECHO message, which would display message

Note: if continually testing the same batch file, you may begin to get errors as the change the batch file makes may already have been done.
Example: if your batch file creates a directory called lab3, the second time your run the batch file it will fail, as there is already a directory named lab 3.
Thus, you may have to manually UNDO all the changes your batch file makes during the testing of your batch file.

Finally, many commands require a source or destination to work. Since you may not know in advance what the source or desination may be, you could use %1 anytime you will specify a source when you run the command, or %2 anytime you will specify a destination when you run the command.

An example is how the MOVE command works.
Syntax MOVE source destination
MOVE copies the file to a different place, then gets rid of the original. You could write this same functionality with a batch file called mov.bat, whose syntax would be MOV.BAT source destination
For details, see replaceable parameters (%1 %2)