• Keine Ergebnisse gefunden

Batch Processing Commands

Im Dokument gj IJ (Seite 56-68)

Batch Processing Commands

Now you have seen some of the capabilities of batch files. In this section you'll find out how to add power and flexibility to your batch programs by using batch processing commands. The following table lists these batch commands and describes briefly what they do:

Command

Perfonns a command for a set of files.

Processes commands starting with the line after the specified label.

Perfonns a command if a condition is met.

Pauses during the processing of a batch file.

Displays a comment in a batch file.

Increases the number of replaceable parameters in a batch process.

Each of these commands is described in the pages that follow.

Batch Processing Commands

echo

The echo command turns the batch echo feature on and off.

Syntax echo [ON]

or echo [OFF]

or

echo [message]

Description

Normally, commands in a batch file are displayed ("echoed") on the screen when they are received by MS-DOS. You can tum off this feature by using the OFF option with the echo command. Similarly, you can tum the echo feature back on by using the ON option with echo.

If you do not specify ON or OFF, echo displays the current setting.

The command echo message (where message is a line of text) is only use-ful if echo is off and if you are using a batch file. If, in your batch file, you type the echo command followed by a message, you can print messages on your screen. You can also put several echo message commands in your batch file to display a message that is several lines in length.

Example

The following is an example of a batch file message of more than one line:

echo off

Batch Processing Commands

for

The for command perfonns a command for a set of files.

Syntax

The set is command sequentially sets the % %c variable to each member of set, and uses the variable to evaluate command. If a member of set is an expression involving a wildcard (* or ?), then the variable is set to each matching item from the disk. In this case, only one such item is in set, so the command ignores any item other than the first.

Examples

The following example binds the variable %f to files ending with *.asm in the working directory.

for % %f in (*.asm) do masm % %f It then executes a command of the following fonn:

masmfilename

filename could be anyone of the following:

invoice.asm receipts.asm taxes.asm

The following example binds the variable %f to the files named report, memo, and address; it then deletes each of these files:

Batch Processing Commands

You must use two percent signs (% %) so that one will remain after the batch parameter (%0 through %9) processing is complete. If you had only %f, instead of % %f, then the batch parameter processor would see tlie %, look at f, decide that %f was an error (a bad parameter reference), and throw out the %f so that the for command would never see it.

If the for command is not in a batch file, you should use only one percent sign.

Batch Processing Commands

goto

The goto label command processes commands starting with the line after the specified label.

Syntax goto label Description

goto lets you take commands from the batch file beginning with the line after the label, where a label is defined as the characters following goto.

This label may include spaces, but not other separators, such as semi-colons or equal signs. If your batch file does not contain the label, the batch file terminates.

Note

Any line in a batch file that starts with a colon (:) is ignored during batch processing.

Example

The following example sends the program processor to the label named end only if no errors occur when you format the disk in drive A:

: begin echo off format a: /s

if errorlevel 0 goto end

echo An error occurred during formatting.

:end

echo End of batch file.

Batch Processing Commands

if

The if command perfonns a command based on the result of a condition.

Syntax

if [NOT] errorlevel number command or

if [NOT] stringl

==

string2 command or

if [NOT] exist filename command Description

The if statement allows conditional execution of commands. When the condition is true, MS-DOS executes the command, otherwise it ignores the command.

The conditions are described as follows:

errorlevel number

True if, and only if, the previous program executed by command. com had an exit code of number or higher. (When a program finishes, it returns an exit code via MS-DOS.) You can use this condition to perfonn other tasks that are based on the previous program's exit code.

stringl

==

string2

True if, and only if, stringl and string2 are identical after parameter sub-stitution. Strings may not contain separators, such as commas, semi-colons, equal signs, or spaces.

Batch Processing Commands

Example

The following example prints the message "can't find data file" if the file product.dat does not exist on the disk:

if not exist product.dat echo can't find datafile

a

Batch Processing Commands

pause

The pause command suspends execution of a batch file.

Syntax

pause [comment]

Description

When a batch file is running, you may need to change disks or perforn some other action. The pause command suspends execution of the batd file until you press any key, unless you press the <CTL>c key sequence.

When the command processor encounters pause, it prints the followin~

message:

If you press <CTL>c, MS-DOS displays the following message:

I'

Tanoioa'a ba'oh job "/NI'

If you type Y in response to this prompt, the batch file ends and control returns to the operating system. Therefore, you can use pause to divide a batch file into pieces that allow you to end the batch command file at any intermediate point.

Batch Processing Commands

Note

The pause and comment line of your batch file will not appear if echo is off.

Example

Suppose you want a program to display a message that asks the user to change disks in one of the drives. To do this you might use the following command:

pause Please put a new disk into drive A

If echo is on, this line will precede the "Strike a key" message when you

run the batch file.

II

Batch Processing Commands

rem

During execution of a batch file, rem displays remarks that are on the same line as the rem command in that batch file.

Syntax rem [comment]

Description

The comment parameter is a line of text that helps you identify anc remember what your batch file does.

The only separators allowed in the comment are spaces, tabs, and com·

mas.

In your batch file, you can use rem without a comment to add spacing fOl readability.

Example

The following example shows a batch file that uses remarks for both explanation and spacing:

rem This file formats and checks new disks rem It is named checknew.bat

rem

pause Insert new disk in drive B format B: Iv

chkdsk B:

Batch Processing Commands

shift

The shift command lets you change the position of replaceable parame-ters in batch file processing.

Syntax shift

Description

You can use the shift command to change the poSitions of (replaceable) command line parameters.

Usually command files are limited to handling ten parameters, %0

through %9. By using shift, you can access more than ten parameters.

II

This means that if there are more than ten parameters given on a com- ~

mand line, those that appear after the tenth (%9) will be shifted one at a time into %9.

You can use the shift command even if you have less than ten parameters.

There is no backward shift command. Once you have executed shift, you cannot recover the first parameter (%0) that existed before the shift command.

Batch Processing Commands

Example

The following file mycopy.bat shows how to use the shift command with any number of parameters. It copies a list of files to a specific directory.

rem rnycopy.bat copies rem any number of files rem to a directory.

rem The command is rem mycopy dir files :one

if "%1" = " " goto two set todir = %1

shift

copy %1 %todir%

goto one :two set todir=

echo All done

Chapter 5

Im Dokument gj IJ (Seite 56-68)