advanced batch files
DESCRIPTION
Chapter 11. Advanced Batch Files. Overview. Quick review of batch file commands learned in earlier chapters. Overview. Advanced features of these commands will be explained and used. Overview. - PowerPoint PPT PresentationTRANSCRIPT
Ch 11 1
Advanced Batch Files
Ch 11 2
OverviewOverview
Quick review of batch file commands learned in earlier
chapters.
Ch 11 3
OverviewOverview
Advanced features of these commands will be explained
and used.
Ch 11 4
OverviewOverview
Will explain the purpose and function of remaining batch file commands
and then will use these commands to write sophisticated batch files.
Ch 11 5
OverviewOverview
Will refine techniques in working with environment.
Ch 11 6
Batch File CommandsBatch 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
Ch 11 7
Batch File CommandsBatch File Commands
Any command used at the command line can be used in a
batch file.
Ch 11 8
Batch File CommandsBatch File CommandsTable 11.1 Batch File Commands p. 548Table 11.1 Batch File Commands p. 548
Ch 11 9
Batch File CommandsBatch File CommandsTable 11.1 Batch File Commands p. 548Table 11.1 Batch File Commands p. 548
Ch 11 10
Batch File CommandsBatch File Commands
Batch files have: Limited vocabulary (commands) Syntax Programming logic
Ch 11 11
Review of REM, PAUSE, Review of REM, PAUSE, and ECHO Commandsand 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
Ch 11 12
Review of REM, PAUSE, Review of REM, PAUSE, and ECHO Commandsand 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
Ch 11 13
Review of REM, PAUSE, Review of REM, PAUSE, and ECHO Commandsand ECHO Commands
To interrupt a batch file during execution:
Press <Ctrl> + C Press <Ctrl> + <Break>
Ch 11 14
Review of REM, PAUSE, Review of REM, PAUSE, and ECHO Commandsand ECHO Commands
ECHO command: Used on command line or in
batch file Controls printing of messages on
screen when batch file run
Ch 11 15
Review of REM, PAUSE, Review of REM, PAUSE, and ECHO Commandsand 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.
Ch 11 16
Advanced Features of Advanced Features of ECHO and REMECHO and REM
For faster processing, use a double colon (::) instead of
“REM” in front of remark or documentation line.
Ch 11 17
Advanced Features of Advanced Features of ECHO and REMECHO and REM
To delete the display of even the message “1 file(s) copied”, redirect output of command to
NUL device.
Ch 11 18
Advanced Features of Advanced Features of ECHO and REMECHO and REM
Using NUL will not suppress a message such as “file not
found”.
Ch 11 19
Advanced Features of Advanced Features of ECHO and REMECHO 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.
Ch 11 20
Advanced Features of Advanced Features of ECHO and REMECHO and REM
To insert a blank line, key in ECHO followed by a period
(ECHO.)
Ch 11 21
Activity—Using ECHO Activity—Using ECHO and NULand 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
Ch 11 22
The GOTO CommandThe GOTO Command
GOTO command: In conjunction with a label creates a
loop Processes command following label
Ch 11 23
The GOTO CommandThe GOTO Command
Loop repeats steps until stopped by . . . using an IF statement. breaking into the batch file with
<Ctrl> + C.
Ch 11 24
The GOTO CommandThe 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
Ch 11 25
The GOTO CommandThe GOTO Command
GOTO has one parameter: GOTO label
Ch 11 26
Activity—Using the Activity—Using the GOTO CommandGOTO 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
Ch 11 27
Activity—Using the Activity—Using the GOTO CommandGOTO Command Example of a Batch file to delete
all files from many floppy disks@ECHO OFF:TOPCLSECHO Place disk with files no longer want in ECHO Drive A.PAUSEDEL /Q A:*.*\ECHO Press Ctrl + C to stop executing this batch file.ECHO otherwise, press any key to continue deleting files.PAUSE > NULGOTOTOP
Ch 11 28
The SHIFT CommandThe SHIFT Command
SHIFT command allows for an unlimited number of
parameters on the command line.
Ch 11 29
Activity—Using the Activity—Using the Shift CommandShift Command
Ch 11 30
Activity—Using the Activity—Using the Shift CommandShift Command
Ch 11 31
Activity—Using the Activity—Using the Shift CommandShift Command
Ch 11 32
Activity—Using the Activity—Using the Shift CommandShift Command
Ch 11 33
Activity—Using the Activity—Using the Shift CommandShift 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
Ch 11 34
Activity—Using the Activity—Using the Shift CommandShift 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
Ch 11 35
Activity—Using the Activity—Using the Shift CommandShift 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”
Ch 11 36
The IF CommandThe 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.
Ch 11 37
The IF CommandThe IF CommandResults 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
Ch 11 38
The IF CommandThe 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
Ch 11 39
The IF CommandThe IF CommandIF 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
Ch 11 40
The IF Command The IF Command Using StringsUsing Strings
IF can be used to compare strings.
Two equal signs (= = ) separate items to be compared.
Ch 11 41
The IF Command The IF Command Using StringsUsing Strings
Can tell IF statement to GOTO a label or to perform an operation whether the
condition is true or false.
Ch 11 42
Activity—Using the IF Activity—Using the IF Command with StringsCommand 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
Ch 11 43
Testing for NULL ValuesTesting for NULL Values
If SHIFT used in a batch file will be caught in endless loop when all parameters are used.
Ch 11 44
Testing for NULL ValuesTesting for NULL Values
A null value (value equal to “nothing”) must be placed in a batch file to indicate end of
data.
Ch 11 45
Testing for NULL ValuesTesting for NULL Values
Can test for a NULL value using IF with quotation marks.
IF “%1” = = GOTO LABEL
Ch 11 46
Testing for NULL ValuesTesting for NULL Values
Can test for a NULL value using IF with any word.
IF %1word = = word GOTO LABEL
word==word
Ch 11 47
Testing for NULL ValuesTesting for NULL Values
Can test for a NULL value using IF with backslash.
IF \%1\= =\\ GOTO LABEL
\\==\\
Ch 11 48
Activity—Using Activity—Using NULL ValuesNULL Values
KEY CONCEPTS: Test for a null value using
quotation marks Test for null value using a user
designated word
Ch 11 49
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 Works only with file names - not
directory names
Ch 11 50
The IF EXIST/IF NOT The IF EXIST/IF NOT EXIST CommandEXIST 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
Ch 11 51
The IF EXIST/IF NOT The IF EXIST/IF NOT EXIST CommandEXIST 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
Ch 11 52
Activity—Using IF EXIST Activity—Using IF EXIST to Test for a Fileto 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
Ch 11 53
Activity—Using IF EXIST Activity—Using IF EXIST to Test for a Fileto 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
Ch 11 54
Activity—Using IF EXIST Activity—Using IF EXIST to Test for a Fileto 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
Ch 11 55
Activity—Using IF EXIST Activity—Using IF EXIST to Test for a Fileto 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
Ch 11 56
The IF ERRORLEVEL The IF ERRORLEVEL Command TestingCommand Testing
Program can set an exit code when it finishes executing.
Batch file tests exit code with IF ERRORLEVEL statement.
Ch 11 57
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 58
The IF ERRORLEVEL The IF ERRORLEVEL Command TestingCommand Testing
Exit codes listed in descending order when using IF ERROR LEVEL.
Exit codes listed in ascending order when using IF NOT ERORLEVEL.
Ch 11 59
Activity—Using Activity—Using IF IF ERRORLEVELERRORLEVEL with COPY with COPY
KEY CONCEPT: User exit codes successfully in a
batch file
Ch 11 60
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
Exit codes: Set by an operating system
program Created by writing small program
based upon an activity
Ch 11 61
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
Every time a key is pressed, it is identified by a scan code.
Ch 11 62
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
Can write programs by using: Programming language DEBUG (operating system
utility program)
Ch 11 63
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
Easiest way to use DEBUG is to create a script file.
Ch 11 64
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
A script file is set of instructions that can be written
in any text editor.
Ch 11 65
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
Feed script file via redirection into DEBUG program.
DEBUG program converts script file to executable program with .com file extension.
Ch 11 66
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
Summary of command available within BUG program (p. 576)
assemble A (Address)compare C range addressdump D (range)enter E address (list)fill F range listgo G [=address] [addresses]hex H value1 value2input I portload L [address] [drive] [firstsector] [number]move M range addressname N [pathname] [arglist]output O port byte
Ch 11 67
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
Summary of command available within BUG program (p. 576)
proceed P [=address][number]quit Qregister R [register]search S range listtrace 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
Ch 11 68
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
.COM Program Written with DEBUG to return scan code of Pressed Key
A:\>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>
Ch 11 69
Writing Programs to Test Writing Programs to Test for Key Codesfor Key Codes
Easier way to create reply.com is to create script file with any text editorScript file - text file containing series of
commands that can be redirected into DEBUG to create a .COM file
Script file is not the program
Ch 11 70
Activity—Writing a Activity—Writing a Script FileScript 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
Ch 11 71
The EnvironmentThe Environment
The environment is an area in memory where data can be
stored.
Ch 11 72
The EnvironmentThe Environment
Data: Constant/fixed values - never change Variables that do change - depends on
conditions or information passed to program
Ch 11 73
The EnvironmentThe Environment
In programming, an expression is any legal
combination of symbols that represent a value.
Ch 11 74
The EnvironmentThe Environment
The operating system stores data in form of two strings:
Name of variable Value of variable
Ch 11 75
The EnvironmentThe Environment
Environmental variable is name assigned to string (value) of
data.
Ch 11 76
The EnvironmentThe Environment
User can set environmental variables.
Some common variables set when Windows started.
Ch 11 77
The EnvironmentThe Environment
User can leave messages in environment using SET
command.
Ch 11 78
The EnvironmentThe 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.
Ch 11 79
The EnvironmentThe Environment
SET syntax:
SET [ variable = [string] ]
Ch 11 80
The EnvironmentThe Environment
SET without parameters displays current
environmental variables.
Ch 11 81
Activity—Using SET and the Activity—Using SET and the Environmental VariablesEnvironmental 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
Ch 11 82
Activity—Using SET and the Activity—Using SET and the Environmental VariablesEnvironmental 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
Ch 11 83
Using SET and the Using SET and the Environment in Batch FilesEnvironment in Batch Files
Can use built-in environmental variables that Windows sets and uses.
Can set own environmental variables.
Ch 11 84
Using SET and the Using SET and the Environment in Batch FilesEnvironment 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
Ch 11 85
Activity—Using SET and the Activity—Using SET and the Environment in Batch FilesEnvironment 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
Ch 11 86
The DIRCMD The DIRCMD Environmental VariableEnvironmental 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.
Ch 11 87
Activity—Using Activity—Using DIRCMDDIRCMD
KEY CONCEPTS: Can determine how to display batch
files Files displayed this way until change
values or close Command Prompt session
Ch 11 88
FOR…IN…DO CommandFOR…IN…DO Command
FOR..IN..DO command: Allows repetitive processing Can be
issued at the command line placed in a batch file
Ch 11 89
FOR…IN…DO CommandFOR…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.
Ch 11 90
FOR…IN…DO CommandFOR…IN…DO Command
Syntax at command line:
FOR %variable IN (set) DOcommand [command-parameters]
Ch 11 91
FOR…IN…DO CommandFOR…IN…DO Command
Syntax in a batch program:
FOR %%variable IN (set) DOcommand [command-parameters]
Ch 11 92
FOR…IN…DO CommandFOR…IN…DO Command
GOTO loop is vertical.
FOR…IN…DO loop is horizontal.
Ch 11 93
FOR…IN…DO CommandFOR…IN…DO Command
There is a difference between a variable and a parameter.
Ch 11 94
Activity—Using the Activity—Using the FOR…IN…DO CommandFOR…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
Ch 11 95
Activity—Using the FOR…Activity—Using the FOR…IN…DO CommandIN…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
Ch 11 96
More Features of the More Features of the FOR…IN…DO CommandFOR…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)
Ch 11 97
More Features of the FOR…More Features of the FOR…IN…DO CommandIN…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
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 Activity—Using Additional Features of FOR…IN…DO Features of FOR…IN…DO
CommandCommand
Ch 11 99
Activity—Using Additional Activity—Using Additional Features of FOR…IN…DO Features of FOR…IN…DO
CommandCommand
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
Ch 11 100
The CALL CommandThe 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
Ch 11 101
Activity—Using CALLActivity—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
Ch 11 102
Activity—Using CALLActivity—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>