Ch 111 Chapter 11 Advanced Batch Files. Ch 112 Overview This chapter focuses on batch file commands that allow you to: write sophisticated batch files.

Post on 24-Dec-2015

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

Ch 11 1

Chapter 11Chapter 11

Advanced Batch Files

Ch 11 2

OverviewOverviewThis chapter focuses on batch file commands that allow you to: write sophisticated batch

files further refine your technique in working with the environment

Ch 11 3

Batch File Batch File CommandsCommands

Batch files must … have the file extension .BAT be an ASCII text file include legitimate commands

Ch 11 4

Batch File Batch File CommandsCommands

Any command used at the command line can be used

in a batch file.

Ch 11 5

Batch File Batch File CommandsCommands

Batch files have … a limited vocabulary (commands) a syntax programming logic

Ch 11 6

A Review of the A Review of the REMREM, , PAUSEPAUSE, and , and ECHOECHO

CommandsCommands

REM command (remarks)…statements keyed in by user that tell what is the purpose of the batch

file.

Ch 11 7

A Review of the A Review of the REMREM, , PAUSEPAUSE, and , and ECHOECHO

CommandCommand

PAUSE command …instructs the batch file

to stop executing until the user takes some action.

Ch 11 8

A Review of the A Review of the REMREM, , PAUSEPAUSE, and the , and the ECHOECHO

CommandCommand

Two ways to interrupt a batch file during execution:

Press <Ctrl> + C Press <Ctrl> + <Break>

Ch 11 9

A Review of the A Review of the REMREM, , PAUSEPAUSE, and the , and the ECHOECHO

CommandCommand

ECHO command …displays a command

and the output of thatcommand to the screen.

Ch 11 10

A Review of the A Review of the REMREM, , PAUSEPAUSE, and the , and the ECHOECHO

CommandCommand

When ECHO is ON: all commands in a batch file are displayed on the screen.

Ch 11 11

A Review of the A Review of the REMREM, , PAUSEPAUSE, and the , and the ECHOECHO

CommandCommand

When ECHO is OFF … can see the output of the command, but not the command itself.

Ch 11 12

Advanced Features Advanced Features of of ECHOECHO and and REMREM

To save valuable processing time, use a double colon (::)

instead of the REM command.

Ch 11 13

Advanced Features Advanced Features of ECHO and REMof ECHO and REM

To delete the display of even the message “1 file(s)

copied”, use the device NUL.

Ch 11 14

Advanced Features Advanced Features of ECHO and REMof ECHO and REM

Using NUL will not suppress a message such

as “file not found”.

Ch 11 15

Advanced Features Advanced Features of ECHO and REMof ECHO and REM

To suppress all messages use the command CTTY

NUL.

Ch 11 16

Advanced Features Advanced Features of ECHO and REMof ECHO and REM

When CTTY NUL is used, must follow with CTTY

CON in order to have control of the console.

Ch 11 17

Advanced Features Advanced Features of ECHO and REMof ECHO and REM

There is no such thing as a blank line in batch files.

Ch 11 18

Advanced Features Advanced Features of ECHO and REMof ECHO and REM

To insert a blank line, key in ECHO followed by a

period (ECHO.)

Ch 11 19

Activity:Activity: Using ::,Using ::,ECHOECHO, , CTTYCTTY, and, and

NULNUL

. . .

Ch 11 20

The GOTO The GOTO CommandCommand

GOTO command … will branch to a new line, creating a loop works in conjunction with a label

Ch 11 21

The GOTO The GOTO CommandCommand

A loop will repeat steps until it is stopped by …

using an IF statement breaking into the batch file with <Ctrl> + C

Ch 11 22

The GOTO The GOTO CommandCommand

A label … is preceded by a colon (:) can be no longer than 8 characters is not a command

Ch 11 23

The GOTO The GOTO CommandCommand

GOTO has one parameter … GOTO label

Ch 11 24

Activity:Activity: Using theUsing theGOTO CommandGOTO Command

. . .

Ch 11 25

The SHIFT The SHIFT CommandCommand

The SHIFT command allows for an unlimited

number of parameters on the command line.

Ch 11 26

The SHIFT The SHIFT CommandCommand

SHIFT command …changes the position of the replaceable

parameterin a batch file.

Ch 11 27

Activity:Activity: Using theUsing theSHIFT CommandSHIFT Command

. . .

Ch 11 28

The IF CommandThe IF Command

IF command …allows for conditional

processing.

Ch 11 29

The IF CommandThe IF Command

Conditional processing …compares two items to determine if they areidentical, or if one isgreater than the other.

Ch 11 30

The IF CommandThe IF Command

The result of comparison testing is either a True or

False value.

Ch 11 31

The IF CommandThe IF Command

True = items are identical

False = items are not identical

Ch 11 32

The IF CommandThe IF Command

Syntax of IF command:

IF <condition> <command>

Ch 11 33

The IF CommandThe IF Command

If a condition is True, the command will be executed.

If a condition is False, the command will not be executed.

Ch 11 34

The IF CommandThe IF Command

The IF command checks for three conditions.

Ch 11 35

IF Command IF Command Using StringsUsing Strings

To test whether or not one character string is exactly the same as another, use

the IF command.

Ch 11 36

IF Command IF Command Using StringsUsing Strings

The strings to be compared are separated by two

equal signs (= =).

Ch 11 37

Activity:Activity: Using theUsing theIF Command withIF Command with

StringsStrings

. . .

Ch 11 38

Testing for NULL Testing for NULL ValuesValues

In a batch file it is sometimes possible to be caught in an endless loop.

Ch 11 39

Testing for NULL Testing for NULL ValuesValues

A value equal to “nothing” must be placed in a batch file to indicate when to

end.

Ch 11 40

Testing for NULL Testing for NULL ValuesValues

NULL values …a user-defined value

equivalent to nothing (no data).

Ch 11 41

Testing for NULL Testing for NULL ValuesValues

Testing for a null value involves using the IF

command with quotation marks.

Ch 11 42

Testing for NULL Testing for NULL ValuesValues

Use quotation marks to test for null value:

IF “%1” = = GOTO LABEL

Ch 11 43

Testing for NULL Testing for NULL ValuesValues

“If nothing is there, GOTO somewhere else”

Ch 11 44

Activity:Activity: UsingUsingNull ValuesNull Values

. . .

Ch 11 45

The IF EXIST/IF NOT The IF EXIST/IF NOT EXIST CommandEXIST Command

IF EXIST/IF NOT EXIST command:

checks for the existence or non-existence of a

file.

Ch 11 46

Activity:Activity: Using IFUsing IFEXIST to Test for aEXIST to Test for a

FileFile

. . .

Ch 11 47

The IF ERRORLEVEL The IF ERRORLEVEL Command TestingCommand Testing

Exit code …a code set by a program

that indicates a True or False condition when

the program finishesexecuting.

Ch 11 48

The IF ERRORLEVEL The IF ERRORLEVEL Command TestingCommand Testing

IF ERRORLEVEL command…a statement in a batch

file that can test exit codes.

Ch 11 49

The IF ERRORLEVEL The IF ERRORLEVEL Command TestingCommand Testing

An exit code is tested with ERRORLEVEL to

determine if it is greater than or equal to it.

Ch 11 50

Activity:Activity: Using Using IFIFERRORLEVELERRORLEVEL with with

XCOPYXCOPY

. . .

Ch 11 51

Writing Programs to Writing Programs to Test for Key CodesTest for Key Codes

Exit codes can be … set by an operating system program created by writing a small program

Ch 11 52

Writing Programs to Writing Programs to Test for Key CodesTest for Key Codes

To write a program a user can …

use a programming language

use an OS utility called DEBUG

Ch 11 53

Writing Programs to Writing Programs to Test for Key CodesTest for Key Codes

Easiest way to use DEBUG is to create a script file.

Ch 11 54

Writing Programs to Writing Programs to Test for Key CodesTest for Key Codes

Script file …a set of instructions that

can be written in any text

editor.

Ch 11 55

Activity:Activity: Writing aWriting aScript FileScript File

. . .

Ch 11 56

The CHOICE The CHOICE CommandCommand

CHOICE command …

prompts the user to make a choice.

Ch 11 57

The CHOICE The CHOICE CommandCommand

Syntax for CHOICE command:

CHOICE [/C[:]choices] [/N] [/S][/T[:]c,nn] [text]

Ch 11 58

Activity:Activity: UsingUsingCHOICECHOICE

. . .

Ch 11 59

The EnvironmentThe Environment

The environment … is an area that the OS sets aside in memory holds important info. that the OS needs to know

Ch 11 60

The EnvironmentThe Environment

Application programs can … read any items in the environment post their own messages there

Ch 11 61

The EnvironmentThe EnvironmentA user can leave a message there through …

the AUTOEXEC.BAT file other batch files the command line

Ch 11 62

The EnvironmentThe Environment

To leave a message, use the SET command.

Ch 11 63

The EnvironmentThe Environment

Syntax for SET command …

SET [ variable = [string] ]

Ch 11 64

Activity:Activity: Using SETUsing SETand the Environmentand the Environment

in Batch Filesin Batch Files

. . .

Ch 11 65

The DIRCMD The DIRCMD Environmental Environmental

VariableVariable

The DIRCMD command variable allows the user to preset, or change the way DIR displays information.

Ch 11 66

Activity:Activity: UsingUsingDIRCMDDIRCMD

. . .

Ch 11 67

The FOR..IN..DO The FOR..IN..DO CommandCommand

The FOR..IN..DO command can be …

issued at the command line placed in a batch file

Ch 11 68

The FOR..IN..DO The FOR..IN..DO CommandCommand

FOR allows the use of a single command to issue

several commands at once.

Ch 11 69

The FOR..IN..DO The FOR..IN..DO CommandCommand

Syntax for the FOR..IN..DO command at the command line:

FOR %variable IN (set) DOcommand [command-parameters]

Ch 11 70

The FOR..IN..DO The FOR..IN..DO CommandCommand

Syntax for the FOR..IN..DO command in a batch program:

FOR %%variable IN (set) DOcommand [command-parameters]

Ch 11 71

Activity:Activity: Using theUsing theFOR..IN..DOFOR..IN..DO

CommandCommand

. . .

Ch 11 72

The CALL The CALL CommandCommand

CALL command …calls (executes) one batch program from anotherwithout causing firstbatch program to stop.

Ch 11 73

Activity:Activity: UsingUsingCALLCALL

. . .

top related