Disk Operating System (DOS)
An introduction to writing and using DOS batch files which includes more advanced techniques such as redirection, replaceable parameters, branching logic (IF, GOTO), LOOPing, and concatenation.
DOS Batch Files
Batch files are used to automate groups of commands which would ordinarily be executed from the command line.
Benefits of using batch files include:
Common uses for batch files include:
A common practice is to place all batch files in one directory and then PATH to that directory. This centralization of batch files makes modifications easier if they become necessary.
Batch files are plain text files created using a plain text editor such as Notepad or the DOS EDIT text editor.
Each DOS command, and/or other external command, is placed on its own line along with all the required parameters and switches.
Batch files are executed by the command interpreter, Command.com. Command.com opens a batch file, reads the command on the first line, closes the batch file, executes the first command, and then repeats those three steps for each line in the batch file.
This rather odd method of execution can be demonstrated with the following batch file:
@echo off cls echo this is the test.bat batch file pause cls echo hey! look at this! >>test.bat cls echo this is the last line of the test.bat... or is it? Pause
Commands Specific to Batch Files
Echo is a feature of batch files which repeats (echos) each line of the batch file to the screen as it is executed.
Although useful for debugging purposes, once production is completed ECHO serves no purpose other than creating a messy screen which may confuse unsophisticated users.
To "clean up" the batch file operation, turn off ECHO by placing the ECHO OFF command on the first line of the batch file. Since the command does not take effect until after it is executed, DOS will echo that first line. To suppress the echo of that first line, place "@" in front of it. The "@" command can also be used to selectively repress echo as required.
The ECHO command also can be used to display text within a batch file to the screen. Text on a line in a batch file which is preceded by ECHO will appear on the screen regardless of whether or not the ECHO OFF command has been issued.
The command processor will ignore any line in a batch file which is preceded by the Rem command. The Rem command (Rem-ark) is frequently used at the beginning of lines which are intended to document a batch file's operation. It can also be used to disable a selected line or lines for debugging purposes.
When a program (.com or .exe) is run from within a batch file, control returns to the batch file when the program is terminated. But when a command in a batch file is the name of another batch file, control does not return to the first batch. Rather, when the second batch file has terminated it will return to the command line.
Here is an illustrated of this. As written, the last line of the first of the first batch file below will never execute:
@echo off rem Testone.bat cls echo This is testone.bat rem Execute a second batch file testtwo.bat echo This line of Testone.bat will NOT be executed
To make control return to the first batch file, insert the CALL command in front of the command to execute Testtwo.bat. After Testtwo.bat terminates, control returns to Testone.bat and execution picks up where it left off. This time the last line WILL be executed.
@echo off rem Testone.bat cls echo This is testone.bat rem Execute a second batch file CALL testtwo.bat echo This line of Testone.bat WILL be executed
Batch File Examples
The following batch file examples can be pasted into a text editor and modified to match your environment. If you paste them into a word processor, be sure that you Save As a plain text file.
Displaying a text file
When large amounts of text are to be displayed to the screen from within a batch file, a more convenient alternative to ECHOing text to the screen is to place the text in a separate file and then display the contents of that file:
TYPE afile.txtOr, when there is more text that can fit on one screen:
MORE < afile.txt
The second example takes advantage of redirection, see below.
Deleting Temporary Files
Place the following lines in an AUTOEXEC.BAT to clean out temporary files during the boot. (Note the use of redirection, covered in detail below).
del c:\windows\recent\*.* < d:\batch\yes del c:\windows\internet temporary files\cookies\*.* < d:\batch\yes
Control a Printer
Use a one-line batch file like this one to issue a form feed to a HP laser printer:
@echo off rem feed.bat rem issues a form feed to a HP laser printer on LPT1 rem represents ascii char #27, control character for printers echo .&l0H > lpt1
Find a Disk File
A batch file like the following one can be used to find a disk file from the command line.
The /v switch for CHKDSK is for "Verbose", which will list all files on the specified disk. That output is piped (|) through the FIND filter.
The replaceable parameters %1 and %2 stand in for the drive designation and the filename being searched for and make the batch file more flexible. (See replaceable parameters below for more information about them).
rem FF.bat, findfile.bat rem Usage: FF D: filename.ext CHKDSK %1 /v | FIND "%2"
Advanced Batch File Techniques
All DOS commands have some output. For example, the output of the DIR command is a directory listing, while the output of the COPY command is "1 file(s) Copied".
The default destination for all command output is the device named CON - the console, or monitor. Command output can, however, be redirected to other DOS devices such as PRN, NUL, or a disk file. The signs of redirection are > and <.
Redirection works because DOS treats all devices as file handles. Study the
examples in the table below:
Bruce Miller, 2002, 2014