Advanced%20Batch%20Files - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced%20Batch%20Files

Description:

4. ECHO for the new directory name. 5. GOTO end. 6. :next. Ch 11. 53 ... False condition - command ignored - next line in batch file processed ... – PowerPoint PPT presentation

Number of Views:230
Avg rating:3.0/5.0
Slides: 103
Provided by: kathryne1
Category:

less

Transcript and Presenter's Notes

Title: Advanced%20Batch%20Files


1
Chapter 11
  • Advanced Batch Files

2
Overview
  • Quick review of batch file commands learned in
    earlier chapters.

3
Overview
  • Advanced features of these commands will be
    explained and used.

4
Overview
  • Will explain the purpose and function of
    remaining batch file commands and then will use
    these commands to write sophisticated batch
    files.

5
Overview
  • Will refine techniques in working with
    environment.

6
Batch File Commands
  • Batch files
  • Have file extension .BAT or .CMD
  • Are ASCII text files
  • Include legitimate commands
  • Create generic batch files using replaceable
    parameters
  • Are not case sensitive

7
Batch File Commands
  • Any command used at the command line can be used
    in a batch file.

8
Batch File CommandsTable 11.1 Batch File
Commands p. 548

9
Batch File CommandsTable 11.1 Batch File
Commands p. 548
10
Batch File Commands
  • Batch files have
  • Limited vocabulary (commands)
  • Syntax
  • Programming logic

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

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

13
Review of REM, PAUSE, and ECHO Commands
  • To interrupt a batch file during execution
  • Press ltCtrlgt C
  • Press ltCtrlgt ltBreakgt

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

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 _at_ and ECHO OFF will not
    appear on screen.

16
Advanced Features of ECHO and REM
  • For faster processing, use a double colon ()
    instead of REM in front of remark or
    documentation line.

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.

18
Advanced Features of ECHO and REM
  • Using NUL will not suppress a message such as
    file not found.

19
Advanced Features of ECHO and REM
  • There is no such thing as a blank line in batch
    files.
  • Pressing ltEntergt does not generate a blank line
    in batch files.

20
Advanced Features of ECHO and REM
  • To insert a blank line, key in ECHO followed by a
    period (ECHO.)

21
ActivityUsing ECHO and NUL
  • KEY CONCEPTS
  • Replaced REM with () for faster processing
  • Redirected output to NUL device so no
    messages/remarks shown on screen
  • _at_ before ECHO OFF - ECHO OFF does not appear
    on screen
  • ECHO. created a blank line in batch file

22
The GOTO Command
  • GOTO command
  • In conjunction with a label creates a loop
  • Processes command following label

23
The GOTO Command
  • Loop repeats steps until stopped by . . .
  • using an IF statement.
  • breaking into the batch file with ltCtrlgt C.

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

25
The GOTO Command
  • GOTO has one parameter
  • GOTO label

26
ActivityUsing 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

27
ActivityUsing the GOTO Command
  • Example of a Batch file to delete
  • all files from many floppy disks
  • _at_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 gt NUL
  • GOTOTOP

28
The SHIFT Command
  • SHIFT command allows for an unlimited number of
    parameters on the command line.

29
ActivityUsing the Shift Command
30
ActivityUsing the Shift Command
31
ActivityUsing the Shift Command
32
ActivityUsing the Shift Command
33
ActivityUsing 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

34
ActivityUsing the Shift Command
  • KEY CONCEPTS
  • sign tells OS to concatenate files
  • Contents of file ended when see EOF
  • Typically ltCtrlgt Z
  • COPY command places second ltCtrlgt Z at end of
    file creates problem
  • Solve by copying file in binary mode

35
ActivityUsing 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
  • gtgt used to see both name of directory and bytes
    in directory.
  • ltCtrlgt C used to break out

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.

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

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

39
The IF Command
  • IF command syntax
  • IF ltconditiongt ltcommandgt
  • IF NOT ERRORLEVEL number command
  • IF NOT string1string2 command
  • IF NOT EXIST filename command
  • Note complete syntax in Appendix H

40
The IF Command Using Strings
  • IF can be used to compare strings.
  • Two equal signs ( ) separate items to be
    compared.

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.

42
ActivityUsing 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

43
Testing for NULL Values
  • If SHIFT used in a batch file will be caught in
    endless loop when all parameters are used.

44
Testing for NULL Values
  • A null value (value equal to nothing) must be
    placed in a batch file to indicate end of data.

45
Testing for NULL Values
  • Can test for a NULL value using IF with quotation
    marks.
  • IF 1 GOTO LABEL

46
Testing for NULL Values
  • Can test for a NULL value using
  • IF with any word.
  • IF 1word word GOTO LABEL
  • wordword

47
Testing for NULL Values
  • Can test for a NULL value using
  • IF with backslash.
  • IF \1\ \\ GOTO LABEL
  • \\\\

48
ActivityUsing NULL Values
  • KEY CONCEPTS
  • Test for a null value using quotation marks
  • Test for null value using a user designated word

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

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

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

52
ActivityUsing 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

53
ActivityUsing 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

54
ActivityUsing 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

55
ActivityUsing 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

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.

57
The IF ERRORLEVEL Command Testing
  • An exit code is tested with ERRORLEVEL to
    determine if it is greater than or equal to it.

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.

59
ActivityUsing IF ERRORLEVEL with COPY
  • KEY CONCEPT
  • User exit codes successfully in a batch file

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

61
Writing Programs to Test for Key Codes
  • Every time a key is pressed, it is identified by
    a scan code.

62
Writing Programs to Test for Key Codes
  • Can write programs by using
  • Programming language
  • DEBUG (operating system utility program)

63
Writing Programs to Test for Key Codes
  • Easiest way to use DEBUG
  • is to create a script file.

64
Writing Programs to Test for Key Codes
  • A script file is set of instructions that can be
    written in any text editor.

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.

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

67
Writing Programs to Test for Key Codes
  • Summary of command available
  • within BUG program (p. 576)
  • proceed P addressnumber
  • 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

68
Writing Programs to Test for Key Codes
  • .COM Program Written with DEBUG
  • to return scan code of Pressed Key
  • A\gtDEBUG
  • -a 100 ltEntergt
  • 158E0100 mov ah,8 ltEntergt
  • 158E0102 int 21 ltEntergt
  • 158E0104 cmp al,0 ltEntergt
  • 158E0106 jnz 10a ltEntergt
  • 158E0108 mov ah,8int21 ltEntergt
  • 158E010A mov ah,4c ltEntergt
  • 158E010C int 21 ltEntergt
  • 158E010E mov ah,8 ltEntergt
  • -r cx ltEntergt
  • CX 0000
  • e ltEntergt
  • -n reply.com ltEntergt
  • -w ltEntergt
  • Writing 000E bytes

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

70
ActivityWriting 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

71
The Environment
  • The environment is an area in memory where data
    can be stored.

72
The Environment
  • Data
  • Constant/fixed values - never change
  • Variables that do change - depends on conditions
    or information passed to program

73
The Environment
  • In programming, an expression is any legal
    combination of symbols that represent a value.

74
The Environment
  • The operating system stores data in form of two
    strings
  • Name of variable
  • Value of variable

75
The Environment
  • Environmental variable is name assigned to string
    (value) of data.

76
The Environment
  • User can set environmental variables.
  • Some common variables set when Windows started.

77
The Environment
  • User can leave messages in environment using SET
    command.

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.

79
The Environment
  • SET syntax
  • SET variable string

80
The Environment
  • SET without parameters displays current
    environmental variables.

81
ActivityUsing SET and the Environmental Variables
  • KEY CONCEPTS
  • Command processor must be in memory when keying
    in command
  • Using MORE
  • Pressing ltEntergt - moves one line at a time
  • Pressing ltSpaceBargt - goes to end of file
  • SET with letter of alphabet - environmental
    variables beginning with letter displayed

82
ActivityUsing 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

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.

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

85
ActivityUsing 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

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.

87
ActivityUsing DIRCMD
  • KEY CONCEPTS
  • Can determine how to display batch files
  • Files displayed this way until change values or
    close Command Prompt session

88
FORINDO Command
  • FOR..IN..DO command
  • Allows repetitive processing
  • Can be
  • issued at the command line
  • placed in a batch file

89
FORINDO 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.

90
FORINDO Command
  • Syntax at command line
  • FOR variable IN (set) DO
  • command command-parameters

91
FORINDO Command
  • Syntax in a batch program
  • FOR variable IN (set) DO
  • command command-parameters

92
FORINDO Command
  • GOTO loop is vertical.
  • FORINDO loop is horizontal.

93
FORINDO Command
  • There is a difference between
  • a variable and a parameter.

94
ActivityUsing the FORINDO 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

95
ActivityUsing the FORINDO 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\BATCHgtA\BATCH\ADD A\BATCH
  • Changed dates of files

96
More Features of the FORINDO Command
  • New features of FORINDO command
  • May list environmental variables so they are
    divided and appear on separate lines
  • Use /R parameter (recursive parameter)

97
More Features of the FORINDO Command
  • New features of FORINDO command
  • Use tilde operator () to
  • Strip a file name of quotation marks
  • Expand variable
  • Select specific text from ASCII files

98

ActivityUsing Additional Features of FORINDO
Command
  • KEY CONCEPTS
  • Use FORINDO 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

99
ActivityUsing Additional Features of FORINDO
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

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

101
ActivityUsing CALL
  • KEY CONCEPTS
  • Use ltCtrlgt L - to eject a page
  • Use ltCtrlgt 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

102
ActivityUsing 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 ltEntergt
  • Line 2 CD gtgt A\BATCH\HOMESAVE.BAT ltEntergt
  • Line 3CALL HOMNESAVE.BAT ltEntergt
  • Line 4 DEL A\BATCH\HOMESAVE.BAT ltEntergt
Write a Comment
User Comments (0)
About PowerShow.com