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

Introduction

Batch files are used to automate groups of commands which would ordinarily be executed from the command line.

Benefits of using batch files include:

  • Savings in time and effort
  • Simplification of operations for inexperienced users
  • Simplification of complex commands for experienced users
  • Elimination of operator error

Common uses for batch files include:

  • Copying or deleting files
  • Creating the proper environment for an application
  • Setting environmental variables

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

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.

Rem

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.

Call

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.txt
Or, 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

Redirection

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:

Redirected Command Result
DIR > PRN Printout of the directory listing on the default printer
DIR > dir.txt A directory listing written to the file DIR.TXT

(If the file DIR.TXT already exists, it is overwritten)
DIR >> dir.txt A directory listing written to the file DIR.TXT

(If the file DIR.TXT already exists, the directory listing is appended to the end of the file)
CHKDSK >> sysinfo.txt The output of CHKDSK is written to the file SYS.INFO.TXT

If the file already exists the output is appended to the end of the file
COPY filename > NUL The output of COPY (1 file(s) Copied) is sent to the NUL device.

The NUL device, otherwise known as the "bit bucket", is a test device which is a virtual dead-end. In other words, the output gets sent nowhere.

This technique is commonly used to hide batch file screen output which cannot otherwise be suppressed with ECHO OFF.


Redirection can also be used to respond to DOS commands. For example, if we put a command to delete all files at some location (DEL [path]\*.*) in a batch file, the delete command will pause the batch file execution to ask "Are You Sure?".

To avoid this we can redirect the contents of a file containing "Y" (yes) and a carriage return (Enter) as input to the DEL command:

First, in the batch file folder, create a text file named YES containing the letter 'y" followed by a carriage return. Then, in a batch file use a line like:

    DEL [path]\*.* < d:\batch\YES

Replaceable parameters

Replaceable parameters pass additional input provided by the user from the command line to a specified place within a batch file. This makes it possible to write more flexible batch files.

Create a batch file such as this:

    REM GO.BAT
    CD \%1
    %2

Execute the batch file this way:

    GO dbase dbase

And the batch file will switch to the \dbase folder and execute dbase.exe.

Execute the batch file this way:

    GO myeditor ed

And the batch file will switch to the \myeditor folder and execute ed.exe.

IF and GOTO

The combination of the IF statement and the GOTO statement can be used to create branching logic within a batch file.

For example, if a batch file is designed to function with a replaceable parameter it is desirable to make a test to see whether or not the user actually entered the parameter at the command line and bail out if none was given.

Study the following example:

    REM If there was no parameter supplied jump to the label NONE
    IF %1x==x GOTO NONE
    REM Otherwise, do some commands and then jump to the label END
    [Batch Commands]
    GOTO END
    :NONE
    ECHO Please supply the required parameter!
    :END
    REM Control returns to the command line here

In this example, IF testing and GOTO <:LABEL> is used to prevent the accidental formatting of C:

    REM FORMAT.BAT  (RENAME FORMAT.EXE XFORMATX.EXE)
    IF %1 == C  GOTO ERROR
    IF %1 == c  GOTO ERROR
    IF %1 == C: GOTO ERROR
    IF %1 == c: GOTO ERROR
    XFORMATX.COM %1
    GOTO END
    :ERROR
    ECHO Please do not try to format Drive C:!
:END

LOOP

The LOOP command permits the creation of batch files that repeat:

    rem A batch file to copy a group of files to several diskettes
    rem Ctrl-C terminates a batch file
    :LOOP
    ECHO Place a new diskette in drive A: or Press Ctrl-C when finished
    PAUSE
    COPY D:\TEMP\*.COM A:
    COPY D:\TEMP\*.EXE A:
    GOTO LOOP

Concatenation

CommandResult
COPY FILE1 + FILE2  FILE3Combines files 1 and 2 in file 3
COPY FILE1 + FILE2Appends file 2 to end of file 1
COPY FILE + Update date and time of file
COPY FILE + /bAs above for a .COM file (/b ignores Ctrl Z)



Bruce Miller, 2002, 2014