<<

Older Operating Systems

Class Notes # 9 Batch Files (Part 1) October 21, 2003

DOS enables you to automate your use of DOS commands by creating batch files. A batch file is a text (ASCII) file containing a series of commands that you want DOS to execute. When you type the batch file name, DOS executes these commands in the order they occur within the batch file. Batch files always have the file name extension .BAT, for example: mybatch.BAT.

Batch processing was developed to make computers more productive. DOS executes the commands in a batch file one line at a time, treating each command as though you issued it individually. Batch files are useful for issuing frequently used commands whose parameters you have trouble remembering. In some way, batch files resembles computer programs. DOS has a host of special commands that are used primarily in batch files. Although you can type some of these commands at the DOS prompt, their main use is in batch files. These commands introduce flow control and simple decision-making capabilities into a batch file.

You can create batch files using the DOS editor, or any other text editing programs. When you create batch files, follow these rules: • The batch file name must end with the .BAT extension. • The batch file name cannot be the same as a program file name or an internal DOS command name. • The batch file can contain any valid DOS commands that you can enter at the DOS command line. • Batch files must be ASCII files. • Use only one command or program name per line in the batch file.

You start a batch file by typing its name at the DOS prompt and then pressing Enter. You can stop a batch file by pressing Ctrl- or Ctrl-Break. DOS will displays the following prompt: Terminate batch job (Y/N)?

DOS includes special commands that often are used in batch files. Table one lists batch file commands.

Table one: Batch file commands Command Action @ Suppresses the display of the command line on-screen. CALL Runs another batch file, when finish it returns to the original batch file. CHOICE Halts processing until a specified key is pressed (Use the IFERRORLEVEL command to determine which key was pressed.). ECHO Turns on of off the display of batch commands as they execute; also can display a message on-screen.

1 FOR..IN..DO Permits the use of the same batch command for several files (loop) GOTO Jumps to the line following the specified label in a batch file. IF Permits conditional execution of a command. PAUSE Displays a message and halts processing until a key is pressed. REM Enables you to insert comments into a batch file that describes the purpose of an operation. Anything after the REM statement is not executed. SHIFT Shifts the command-line parameter one parameter to the left.

Example: Creating a batch file that automates saving files from your C:\SALES directory into your floppy drive and compare the copied files. Save the batch file as COPYCOMP.BAT

Figure one: A simple batch file @ECHO OFF COPY C:\SALES A: FC C:\SALES A:/L

The first line (@ECHO OFF), instructs DOS not to display the batch file’s commands as they are executed. In other words, you do not see the command lines themselves on- screen; you see only the results of their actions. The @ symbol stops display of the current line, and ECHO OFF stops display of all subsequent lines.

The second line (COPY C:\SALES A:), copies the desired files from their source location in C:\SALES to the destination root directory of drive A.

The third line (FC C:\SALES A:/L), compares the copied files with the originals. Although you can use the /V (verify) option with the COPY command, using FC is more thorough.

To run this batch file you simply type COPYCOMP and press enter at the DOS prompt. For this batch file to work properly: the C:\SALES directory must exist on your hard drive and also you should have a diskette in your floppy drive.

Replaceable Parameters A parameter is an additional instruction that defines the task you want a DOS command to perform. Therefore, when you use replacement parameters in a batch file, you can cause the same commands to do different things. DOS allows you to include up to nine parameters after the batch file name. DOS assigns a variable name to each parameter, starting with 1 and going up to 9. DOS always assigns the variable name 0 to the name of the batch file itself.

You can use each variable in your batch file by preceding the variable name with the percentage sign (%). This combination of the percentage sign and the variable name (1 to 9) is called a replaceable parameter.

2 When used in a batch file, each replacement parameter (numbered %0 through %9) holds the place for a parameter in one or more commands of a batch file so that you can provide the actual value of the parameter at the time you execute the batch file.

Example: Suppose you want to make the batch file in figure one more versatile so that you can use it to copy and compare the files in any directory. The batch file would then be:

Figure two: Modifying the simple batch file to include any directory @ECHO OFF COPY %1 %2 FC %1 %2 /L

In comparing figure one and two, the first parameter (e.g. directory C:\SALES) has been replaced by the variable %1 and the second parameter (e.g. A drive) has been replaced by the variable %2. You could now use this batch file to copy and compare the files from any directory to any disk or directory. Therefore, if you typed:

COPYCOMP C:\Spreadsh\mydata.txt C:\Files

The variable %1 = C:\Spreadsh\mydata.txt and the variable %2 = C:\Files.

If you omit the second parameter when you type the batch file name, you will get an error since the COPY command, in the batch file, requires a second parameter to perform the operation successfully (i.e. COPYCOMP C:\Spreadsh\mydata.txt ).

However, you can “shortchange” the batch file by not specifying a sufficient number of parameters to fill every variable marker. For example: figure three shows a further modification to the previous batch file example.

Figure Three: Modifying the simple batch file to include the destination root directory @ECHO OFF COPY %1 C:%2 DEL %1

If you were to type the following command:

COPYCOMP C:\Spreadssh\mydata.txt

The variable %1 = C:\Spreadsh\mydata.txt and the variable %2 = blank. The result would be that: mydata.txt will be copied into the root directory and then mydata.txt in the C:\Spreadsh will be deleted.

In the previous examples, we used the ECHO command to turn off the display of the commands in the batch file as they are being executed. We can also turn on the display of commands by the including the statement: ECHO ON.

3 We could also use the ECHO command to display a message to the user. That is, if you don’t include any arguments with the ECHO command, DOS displays the string you type after ECHO command in the batch file. ECHO can display a message up to 122 characters long.

Branching When you run a batch file, DOS normally processes commands one line at a time, from the beginning of the batch file to the end. You can use commands, such as GOTO, to change the order in which DOS executes batch commands by instructing DOS to branch to a specific line in the file.

The syntax for the GOTO command is:

GOTO label

A batch file label is a separate line that is not a command. A label consists of a colon (:) followed by a label name (one to eight characters). Actually, the label name can be more than eight characters, but DOS will only read the first eight characters. For example,

Figure four: A batch file (infinite.bat) with the GOTO command @ECHO OFF :LOOP ECHO Hello, %1 PAUSE GOTO LOOP

To test this batch file, type infinite George and press enter. The screen will display:

Hello, George Press any key to continue_

If you press any key, DOS will execute the GOTO LOOP command, causing execution of the batch file to return to the line labeled :LOOP. DOS again displays the message and pauses. This batch file is an example of what programmers call an . To abort the batch file, type Ctrl-C or Ctrl Break.

4