73
Ch 11 1 Chapter 11 Chapter 11 Advanced Batch Files

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

Embed Size (px)

Citation preview

Page 1: 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

Ch 11 1

Chapter 11Chapter 11

Advanced Batch Files

Page 2: 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

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

Page 3: 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

Ch 11 3

Batch File Batch File CommandsCommands

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

Page 4: 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

Ch 11 4

Batch File Batch File CommandsCommands

Any command used at the command line can be used

in a batch file.

Page 5: 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

Ch 11 5

Batch File Batch File CommandsCommands

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

Page 6: 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

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.

Page 7: 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

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.

Page 8: 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

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>

Page 9: 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

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.

Page 10: 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

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.

Page 11: 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

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.

Page 12: 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

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.

Page 13: 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

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.

Page 14: 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

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”.

Page 15: 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

Ch 11 15

Advanced Features Advanced Features of ECHO and REMof ECHO and REM

To suppress all messages use the command CTTY

NUL.

Page 16: 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

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.

Page 17: 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

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.

Page 18: 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

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.)

Page 19: 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

Ch 11 19

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

NULNUL

. . .

Page 20: 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

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

Page 21: 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

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

Page 22: 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

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

Page 23: 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

Ch 11 23

The GOTO The GOTO CommandCommand

GOTO has one parameter … GOTO label

Page 24: 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

Ch 11 24

Activity:Activity: Using theUsing theGOTO CommandGOTO Command

. . .

Page 25: 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

Ch 11 25

The SHIFT The SHIFT CommandCommand

The SHIFT command allows for an unlimited

number of parameters on the command line.

Page 26: 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

Ch 11 26

The SHIFT The SHIFT CommandCommand

SHIFT command …changes the position of the replaceable

parameterin a batch file.

Page 27: 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

Ch 11 27

Activity:Activity: Using theUsing theSHIFT CommandSHIFT Command

. . .

Page 28: 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

Ch 11 28

The IF CommandThe IF Command

IF command …allows for conditional

processing.

Page 29: 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

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.

Page 30: 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

Ch 11 30

The IF CommandThe IF Command

The result of comparison testing is either a True or

False value.

Page 31: 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

Ch 11 31

The IF CommandThe IF Command

True = items are identical

False = items are not identical

Page 32: 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

Ch 11 32

The IF CommandThe IF Command

Syntax of IF command:

IF <condition> <command>

Page 33: 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

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.

Page 34: 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

Ch 11 34

The IF CommandThe IF Command

The IF command checks for three conditions.

Page 35: 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

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.

Page 36: 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

Ch 11 36

IF Command IF Command Using StringsUsing Strings

The strings to be compared are separated by two

equal signs (= =).

Page 37: 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

Ch 11 37

Activity:Activity: Using theUsing theIF Command withIF Command with

StringsStrings

. . .

Page 38: 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

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.

Page 39: 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

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.

Page 40: 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

Ch 11 40

Testing for NULL Testing for NULL ValuesValues

NULL values …a user-defined value

equivalent to nothing (no data).

Page 41: 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

Ch 11 41

Testing for NULL Testing for NULL ValuesValues

Testing for a null value involves using the IF

command with quotation marks.

Page 42: 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

Ch 11 42

Testing for NULL Testing for NULL ValuesValues

Use quotation marks to test for null value:

IF “%1” = = GOTO LABEL

Page 43: 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

Ch 11 43

Testing for NULL Testing for NULL ValuesValues

“If nothing is there, GOTO somewhere else”

Page 44: 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

Ch 11 44

Activity:Activity: UsingUsingNull ValuesNull Values

. . .

Page 45: 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

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.

Page 46: 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

Ch 11 46

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

FileFile

. . .

Page 47: 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

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.

Page 48: 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

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.

Page 49: 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

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.

Page 50: 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

Ch 11 50

Activity:Activity: Using Using IFIFERRORLEVELERRORLEVEL with with

XCOPYXCOPY

. . .

Page 51: 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

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

Page 52: 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

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

Page 53: 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

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.

Page 54: 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

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.

Page 55: 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

Ch 11 55

Activity:Activity: Writing aWriting aScript FileScript File

. . .

Page 56: 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

Ch 11 56

The CHOICE The CHOICE CommandCommand

CHOICE command …

prompts the user to make a choice.

Page 57: 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

Ch 11 57

The CHOICE The CHOICE CommandCommand

Syntax for CHOICE command:

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

Page 58: 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

Ch 11 58

Activity:Activity: UsingUsingCHOICECHOICE

. . .

Page 59: 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

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

Page 60: 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

Ch 11 60

The EnvironmentThe Environment

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

Page 61: 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

Ch 11 61

The EnvironmentThe EnvironmentA user can leave a message there through …

the AUTOEXEC.BAT file other batch files the command line

Page 62: 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

Ch 11 62

The EnvironmentThe Environment

To leave a message, use the SET command.

Page 63: 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

Ch 11 63

The EnvironmentThe Environment

Syntax for SET command …

SET [ variable = [string] ]

Page 64: 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

Ch 11 64

Activity:Activity: Using SETUsing SETand the Environmentand the Environment

in Batch Filesin Batch Files

. . .

Page 65: 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

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.

Page 66: 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

Ch 11 66

Activity:Activity: UsingUsingDIRCMDDIRCMD

. . .

Page 67: 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

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

Page 68: 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

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.

Page 69: 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

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]

Page 70: 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

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]

Page 71: 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

Ch 11 71

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

CommandCommand

. . .

Page 72: 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

Ch 11 72

The CALL The CALL CommandCommand

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

Page 73: 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

Ch 11 73

Activity:Activity: UsingUsingCALLCALL

. . .