ch 11 advanced batch files - skagit valley...

102
Ch 11 1 Advanced Batch Files

Upload: others

Post on 05-Jun-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 1

Advanced Batch

Files

Page 2: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 2

Overview

Quick review of batch file commands learned in earlier

chapters.

Page 3: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 3

Overview

Advanced features of these

commands will be explained

and used.

Page 4: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 4

Overview

Will explain the purpose and function of remaining batch file

commands and then will use these commands to write sophisticated

batch files.

Page 5: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 5

Overview

Will refine techniques in

working with environment.

Page 6: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 6

Batch File Commands

Batch files:Have file extension .BAT or .CMDAre ASCII text filesInclude legitimate commandsCreate generic batch files using

replaceable parametersAre not case sensitive

Page 7: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 7

Batch File Commands

Any command used at the command line can be used in a

batch file.

Page 8: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 8

Batch File CommandsTable 11.1 Batch File Commands p. 548

Page 9: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 9

Batch File CommandsTable 11.1 Batch File Commands p. 548

Page 10: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 10

Batch File Commands

Batch files have:

Limited vocabulary (commands)

Syntax

Programming logic

Page 11: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 11

Review of REM, PAUSE, and ECHO Commands

REM command (remarks):Documents batch file

Not a command that executes

With ECHO ON displays but does not execute what follows REM

Placed at beginning of a line in batch or CONFIG.SYS file, REM disables but does not delete line

Page 12: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 12

Review of REM, PAUSE, and ECHO Commands

PAUSE command:Instructs batch file to stop executing

until user takes some action

Does not stop execution of .EXE or COM program

Will not do any conditional processing

Page 13: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 13

Review of REM, PAUSE, and ECHO Commands

To interrupt a batch file during execution:

Press <Ctrl> + C

Press <Ctrl> + <Break>

Page 14: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 14

Review of REM, PAUSE, and ECHO Commands

ECHO command:

Used on command line or in batch file

Controls printing of messages on screen when batch file run

Page 15: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 15

Review of REM, PAUSE, and ECHO Commands

ECHO ON -displays all commands to screen along with output.

ECHO OFF - displays only output of commands to the screen.

Precede ECHO OFF with @ and “ECHO OFF” will not appear on screen.

Page 16: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 16

Advanced Features of ECHO and REM

For faster processing, use a

double colon (::) instead of

“REM” in front of remark or

documentation line.

Page 17: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 17

Advanced Features of ECHO and REM

To delete the display of even the message “1 file(s) copied”, redirect output of command to

NUL device.

Page 18: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 18

Advanced Features of ECHO and REM

Using NUL will not suppress a

message such as “file not

found”.

Page 19: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 19

Advanced Features of ECHO and REM

There is no such thing as a blank

line in batch files.

Pressing <Enter> does not generate

a blank line in batch files.

Page 20: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 20

Advanced Features of ECHO and REM

To insert a blank line, key in

ECHO followed by a period

(ECHO.)

Page 21: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 21

Activity—Using ECHO and NUL

KEY CONCEPTS:

Replaced REM with (::) for faster processing

Redirected output to NUL device so no

messages/remarks shown on screen

@ before ECHO OFF - “ECHO OFF” does

not appear on screen

ECHO. created a blank line in batch file

Page 22: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 22

The GOTO Command

GOTO command: In conjunction with a label creates a

loop

Processes command following label

Page 23: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 23

The GOTO Command

Loop repeats steps until stopped by . . .

using an IF statement.

breaking into the batch file with <Ctrl> + C.

Page 24: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 24

The GOTO Command

Label in a batch file: Is not a command

Identifies location in a batch file

Is preceded by a colon (:)

No longer then 8 characters

Not case sensitive

Ignored by OS until called with GOTO command

Page 25: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 25

The GOTO Command

GOTO has one parameter:

GOTO label

Page 26: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 26

Activity—Using the GOTO Command

KEY CONCEPTS:

Debug - see and repair any errors

To execute a batch file must be at system prompt (not in editor)

Usefulness of loops

Redirecting output to NUL device

Page 27: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 27

Activity—Using the GOTO Command

Example of a Batch file to delete

all files from many floppy disks

@ECHO OFF

:TOP

CLS

ECHO Place disk with files no longer want in ECHO Drive A.

PAUSE

DEL /Q A:*.*\

ECHO Press Ctrl + C to stop executing this batch file.

ECHO otherwise, press any key to continue deleting files.

PAUSE > NUL

GOTOTOP

Page 28: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 28

The SHIFT Command

SHIFT command allows for an

unlimited number of

parameters on the command

line.

Page 29: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 29

Activity—Using the Shift Command

Page 30: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 30

Activity—Using the Shift Command

Page 31: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 31

Activity—Using the Shift Command

Page 32: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 32

Activity—Using the Shift Command

Page 33: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 33

Activity—Using the Shift Command

KEY CONCEPTS: Can keep date log not dependent on file

modification date

Usefulness of SHIFT command

Displays 5 or more parameters and places echoing parameters in batch file

Moves each parameter over by one position

ECHO - echoes what is keyed in

Page 34: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 34

Activity—Using the Shift Command

KEY CONCEPTS: + sign tells OS to concatenate files

Contents of file ended when see EOF

Typically <Ctrl> + Z

COPY command places second <Ctrl> + Z at

end of file creates problem

Solve by copying file in binary mode

Page 35: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 35

Activity—Using the Shift Command

KEY CONCEPTS: + /B switch - tells OS to copy file in binary

mode

Concatenated files with no switches - files copied in text mode

>> used to see both name of directory and bytes in directory.

<Ctrl> + C used to “break out”

Page 36: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 36

The IF Command

IF command allows for conditional

processing of parts of a batch file.

Conditional processing compares

two items to determine if they are

identical.

Page 37: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 37

The IF Command

Results of comparison testing: True Items are identical

Command executed

False Items are not identical

Command not executed

Goes to next command line in batch file

Page 38: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 38

The IF Command

IF command checks to see: If two sets of characters are/are not identical

If a file exists

The value of the variable in ERRORLEVEL

Page 39: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 39

The IF Command

IF command syntax :

IF <condition> <command>

IF [NOT] ERRORLEVEL number command

IF [NOT] string1==string2 command

IF [NOT] EXIST filename command

Note: complete syntax in Appendix H

Page 40: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 40

The IF Command Using Strings

IF can be used to compare

strings.

Two equal signs (= = ) separate

items to be compared.

Page 41: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 41

The IF Command Using Strings

Can tell IF statement to GOTO

a label or to perform an

operation whether the

condition is true or false.

Page 42: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 42

Activity—Using the IF Command with Strings

KEY CONCEPTS:No more lines - return to system prompt

Batch file replaceable parameters get value from position on command line

Case matters

To ignore case add the /I parameter immediately following the IF statement

Page 43: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 43

Testing for NULL Values

If SHIFT used in a batch file

will be caught in endless loop

when all parameters are used.

Page 44: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 44

Testing for NULL Values

A null value (value equal to

“nothing”) must be placed in a batch file to indicate end of

data.

Page 45: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 45

Testing for NULL Values

Can test for a NULL value using IF with quotation marks.

IF “%1” = = GOTO LABEL

Page 46: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 46

Testing for NULL Values

Can test for a NULL value using IF with any word.

IF %1word = = word GOTO LABEL

word==word

Page 47: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 47

Testing for NULL Values

Can test for a NULL value using

IF with backslash.

IF \%1\= =\\ GOTO LABEL

\\==\\

Page 48: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 48

Activity—Using NULL Values

KEY CONCEPTS:

Test for a null value using

quotation marks

Test for null value using a user

designated word

Page 49: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 49

The IF EXIST/IF NOT EXIST Command

IF EXIST/IF NOT EXIST command:

Checks for the existence or non-

existence of a file

Works only with file names - not

directory names

Page 50: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 50

The IF EXIST/IF NOT EXIST Command

Using IF EXIST command:

If file does exist

Condition - true

Processing passes to specified GOTO location

If file does not exist

Condition - false

Batch file reads next line in file

Page 51: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 51

The IF EXIST/IF NOT EXIST Command

Using IF NOT EXIST command:

If file does not exist Condition - true

Processing passes to specified GOTO location

If file does exist Condition -false

Batch file reads next line in file

Page 52: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 52

Activity—Using IF EXIST to Test for a File

1. IF \%1\= =\\ GOTO end

2. IF NOT \%2\= =\\ GOTO next

3. ECHO You must include a destination name

4. ECHO for the new directory name.

5. GOTO end

6. :next

Page 53: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 53

Activity—Using IF EXIST to Test for a File

7. IF EXIST % 1 GOTO message

8. REN % 1 % 2

9. GOTO end

10. :message

11. ECHO This is a file, not a directory.

12. :end

Page 54: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 54

Activity—Using IF EXIST to Test for a File

KEY CONCEPTS: IF command testing for true condition

True condition - command processed

False condition - command ignored - next line in batch file processed

IF NOT command testing for “truth” in reverse

False condition - command processed

True condition - command ignored - next line in batch file processed

Page 55: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 55

Activity—Using IF EXIST to Test for a File

KEY CONCEPTS: IF EXIST

Used to check for existence of a file

Can not be used to check for existence of a

directory

Use NUL to “fool” IF EXIST/IF NOT EXIST to check for existence of directory

Page 56: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 56

The IF ERRORLEVEL Command Testing

Program can set an exit code when it

finishes executing.

Batch file tests exit code with IF

ERRORLEVEL statement.

Page 57: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 57

The IF ERRORLEVEL Command Testing

An exit code is tested with ERRORLEVEL to determine if it is greater than or equal to it.

Page 58: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 58

The IF ERRORLEVEL Command Testing

Exit codes listed in descending order

when using IF ERROR LEVEL.

Exit codes listed in ascending order

when using IF NOT ERORLEVEL.

Page 59: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 59

Activity—Using IF ERRORLEVEL with COPY

KEY CONCEPT:

User exit codes successfully in a

batch file

Page 60: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 60

Writing Programs to Test for Key Codes

Exit codes:

Set by an operating system program

Created by writing small program based upon an activity

Page 61: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 61

Writing Programs to Test for Key Codes

Every time a key is pressed, it is

identified by a scan code.

Page 62: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 62

Writing Programs to Test for Key Codes

Can write programs by using:

Programming language

DEBUG (operating system

utility program)

Page 63: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 63

Writing Programs to Test for Key Codes

Easiest way to use DEBUG

is to create a script file.

Page 64: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 64

Writing Programs to Test for Key Codes

A script file is set of

instructions that can be written

in any text editor.

Page 65: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 65

Writing Programs to Test for Key Codes

Feed script file via redirection into DEBUG program.

DEBUG program converts script file to executable program with .com file extension.

Page 66: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 66

Writing Programs to Test for Key Codes

Summary of command available

within BUG program (p. 576)assemble A (Address)

compare C range address

dump D (range)

enter E address (list)

fill F range list

go G [=address] [addresses]

hex H value1 value2

input I port

load L [address] [drive] [firstsector] [number]

move M range address

name N [pathname] [arglist]

output O port byte

Page 67: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 67

Writing Programs to Test for Key Codes

Summary of command available

within BUG program (p. 576)proceed P [=address][number]

quit Q

register R [register]

search S range list

trace T [=address] [value]

unassemble U [range]

write W {address] [drive] [firstsector] [number]

allocate expanded memory XA [#pages]

deallocate expanded memory XD [handle]

map expanded memory pages XM [Lpage] [Ppage] [handle]

display expanded memory status XS

Page 68: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 68

Writing Programs to Test for Key Codes

.COM Program Written with DEBUG

to return scan code of Pressed KeyA:\>DEBUG-a 100 <Enter> 158E:0100 mov ah,8 <Enter>158E:0102 int 21 <Enter>158E:0104 cmp al,0 <Enter>158E:0106 jnz 10a <Enter>158E:0108 mov ah,8int21 <Enter>158E:010A mov ah,4c <Enter>158E:010C int 21 <Enter>158E:010E mov ah,8 <Enter>-r cx <Enter>CX 0000:e <Enter>-n reply.com <Enter>-w <Enter>Writing 000E bytes-q <Enter> <Enter>

Page 69: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 69

Writing Programs to Test for Key Codes

Easier way to create reply.com is to create script file with any text editor

Script file - text file containing series of commands that can be redirected into DEBUG to create a .COM file

Script file is not the program

Page 70: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 70

Activity—Writing a Script File

KEY CONCEPTS:Convert script file into a program by

redirecting it to DEBUG

Error codes tested for equal to or greater than value specified

IF ERRORLEVEL - descending order

IF NOT ERROR LEVEL - ascending order

Page 71: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 71

The Environment

The environment is an area in

memory where data can be

stored.

Page 72: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 72

The Environment

Data:

Constant/fixed values - never change

Variables that do change - depends on

conditions or information passed to

program

Page 73: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 73

The Environment

In programming, an

expression is any legal

combination of symbols that

represent a value.

Page 74: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 74

The Environment

The operating system stores data in

form of two strings:

Name of variable

Value of variable

Page 75: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 75

The Environment

Environmental variable is name assigned to string (value) of

data.

Page 76: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 76

The Environment

User can set environmental

variables.

Some common variables set when

Windows started.

Page 77: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 77

The Environment

User can leave messages in

environment using SET

command.

Page 78: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 78

The Environment

Environmental variables set in

Command Prompt window or batch

files executed in Command Prompt

window remain in effect only during

that command prompt session.

Page 79: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 79

The Environment

SET syntax:

SET [ variable = [string] ]

Page 80: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 80

The Environment

SET without parameters

displays current

environmental variables.

Page 81: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 81

Activity—Using SET and the Environmental Variables

KEY CONCEPTS:Command processor must be in memory

when keying in command

Using MORE

Pressing <Enter> - moves one line at a time

Pressing <SpaceBar> - goes to end of file

SET with letter of alphabet - environmental variables beginning with letter displayed

Page 82: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 82

Activity—Using SET and the Environmental Variables

KEY CONCEPTS: To see value of environmental variable - use

ECHO and enclose environmental variable name you are seeking with percent signs

Environmental variable

Can be used with commands

Can be used to change directories

Page 83: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 83

Using SET and the Environment in Batch Files

Can use built-in environmental

variables that Windows sets and uses.

Can set own environmental variables.

Page 84: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 84

Using SET and the Environment in Batch Files

Setting environmental variables:Can give them name and value in batch

file/command line

Only good for that session of Command

Prompt Window

Page 85: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 85

Activity—Using SET and the Environment in Batch Files

KEY CONCEPTS: Can set environmental value and use it in batch file

Environmental variables set - deleted when

Command Prompt window closed

Environmental variables - not case sensitive

To eliminate value - must set it at nothing

Can add directory to PATH statement

Page 86: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 86

The DIRCMD Environmental Variable

DIRCMD environmental variable with:

SET to preset DIR command parameters or

switches.

ERRORLEVEL to change the way DIR

displays information for current MS-DOS

work session.

Page 87: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 87

Activity—Using DIRCMD

KEY CONCEPTS:

Can determine how to display batch

files

Files displayed this way until change

values or close Command Prompt

session

Page 88: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 88

FOR…IN…DO Command

FOR..IN..DO command: Allows repetitive processing

Can be issued at the command line

placed in a batch file

Page 89: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 89

FOR…IN…DO Command

FOR allows the use of a single command

to issue several commands at once.

Command can DO something FOR every

value IN a specified set.

Page 90: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 90

FOR…IN…DO Command

Syntax at command line:

FOR %variable IN (set) DO

command [command-parameters]

Page 91: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 91

FOR…IN…DO Command

Syntax in a batch program:

FOR %%variable IN (set) DO

command [command-parameters]

Page 92: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 92

FOR…IN…DO Command

GOTO loop is vertical.

FOR…IN…DO loop is horizontal.

Page 93: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 93

FOR…IN…DO Command

There is a difference between a variable and a parameter.

Page 94: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 94

Activity—Using the FOR…IN…DO Command

KEY CONCEPTS: Space and comma between items in a set work

same way

Advantages of using space and comma between items in a set

Command line is case sensitive

Variable letter chosen is not important

% - used at command line

%% used in batch file

Page 95: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 95

Activity—Using the FOR…IN…DO Command

KEY CONCEPTS: Items in set is a horizontal not vertical loop

Moved all batch files to BATCH subdirectory and set path to include BATCH directory

Caution: If close Command Prompt window, have to issue following command to include the A:\BATCH directory in your path: A:\BATCH>A:\BATCH\ADD A:\BATCH

Changed dates of files

Page 96: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 96

More Features of the FOR…IN…DO Command

New features of FOR…IN…DO command:

May list environmental variables so they are

divided and appear on separate lines

Use /R parameter (recursive parameter)

Page 97: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 97

More Features of the FOR…IN…DO Command

New features of FOR…IN…DO command:

Use tilde operator (~) to:

Strip a file name of quotation marks

Expand variable

Select specific text from ASCII files

Page 98: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 98

KEY CONCEPTS: Use FOR…IN…DO to display list one line

at a time (list easier to read)

Tilde (~) strips file name of quotation marks

Use quotation marks to add prefix or suffix to long file name

REN only needs file name

Activity—Using Additional Features of FOR…IN…DO

Command

Page 99: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 99

Activity—Using Additional Features of FOR…IN…DO

Command

KEY CONCEPTS:

Use n and x to precede any file name with

prefix

Can strip out specific fields in a text file -

use /F parameter

Can delimit data

Page 100: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 100

The CALL Command

CALL command:

Allows you to run one batch file from

within another

Returns control to original batch file

when second batch file is finished

executing

Page 101: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 101

Activity—Using CALL

KEY CONCEPTS: Use <Ctrl> + L - to eject a page

Use <Ctrl> + G - to create a noise

Create series of batch files that return to directory you were previously in

When executing a program within a batch file - not returned to batch file unless you use CALL

Page 102: Ch 11 Advanced Batch Files - Skagit Valley Collegefaculty.skagit.edu/imageuploads/repository603.pdf · Ch 11 6 Batch File Commands Batch files: Have file extension .BAT or .CMD Are

Ch 11 102

Activity—Using CALL

KEY CONCEPTS:Creating and Saving batch file called HOMETO.BAT in BATCH directory

Line 1: COPY A:\BATCH\HOME.DAT A:\BATCH\HOMESAVE.BAT <Enter>

Line 2: CD >> A:\BATCH\HOMESAVE.BAT <Enter>

Line 3:CALL HOMNESAVE.BAT <Enter>

Line 4: DEL A:\BATCH\HOMESAVE.BAT <Enter>