Advanced Batch Files Ch 11 1
Overview Quick review of batch file commands learned in earlier chapters. Ch 11 2
Overview Advanced features of these commands will be explained and used. Ch 11 3
Overview Will explain the purpose and function of remaining batch file commands and then will use these commands to write sophisticated batch files. Ch 11 4
Overview Will refine techniques in working with environment. Ch 11 5
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 Ch 11 6
Batch File Commands Any command used at the command line can be used in a batch file. Ch 11 7
Batch File Commands Table 11.1 Batch File Commands p. 548 Ch 11 8
Batch File Commands Table 11.1 Batch File Commands p. 548 Ch 11 9
Batch File Commands Batch files have: Limited vocabulary (commands) Syntax Programming logic Ch 11 10
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 Ch 11 11
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 Ch 11 12
Review of REM, PAUSE, and ECHO Commands To interrupt a batch file during execution: Press <Ctrl> + C Press <Ctrl> + <Break> Ch 11 13
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 Ch 11 14
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. Ch 11 15
Advanced Features of ECHO and REM For faster processing, use a double colon (::) instead of REM in front of remark or documentation line. Ch 11 16
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. Ch 11 17
Advanced Features of ECHO and REM Using NUL will not suppress a message such as file not found. Ch 11 18
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. Ch 11 19
Advanced Features of ECHO and REM To insert a blank line, key in ECHO followed by a period (ECHO.) Ch 11 20
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 Ch 11 21
The GOTO Command GOTO command: In conjunction with a label creates a loop Processes command following label Ch 11 22
The GOTO Command Loop repeats steps until stopped by... using an IF statement. breaking into the batch file with <Ctrl> + C. Ch 11 23
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 Ch 11 24
The GOTO Command GOTO has one parameter: GOTO label Ch 11 25
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 Ch 11 26
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 Ch 11 27
The SHIFT Command SHIFT command allows for an unlimited number of parameters on the command line. Ch 11 28
Activity Using the Shift Command Ch 11 29
Activity Using the Shift Command Ch 11 30
Activity Using the Shift Command Ch 11 31
Activity Using the Shift Command Ch 11 32
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 Ch 11 33
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 Ch 11 34
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 Ch 11 35
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. Ch 11 36
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 Ch 11 37
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 Ch 11 38
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 Ch 11 39
The IF Command Using Strings IF can be used to compare strings. Two equal signs (= = ) separate items to be compared. Ch 11 40
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. Ch 11 41
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 Ch 11 42
Testing for NULL Values If SHIFT used in a batch file will be caught in endless loop when all parameters are used. Ch 11 43
Testing for NULL Values A null value (value equal to nothing ) must be placed in a batch file to indicate end of data. Ch 11 44
Testing for NULL Values Can test for a NULL value using IF with quotation marks. IF %1 = = GOTO LABEL Ch 11 45
Testing for NULL Values Can test for a NULL value using IF with any word. IF %1word = = word GOTO LABEL word==word Ch 11 46
Testing for NULL Values Can test for a NULL value using IF with backslash. IF \%1\= =\\ GOTO LABEL \\==\\ Ch 11 47
Activity Using NULL Values KEY CONCEPTS: Test for a null value using quotation marks Test for null value using a user designated word Ch 11 48
The IF EXIST/IF NOT EXIST Command IF EXIST/IF NOT EXIST command: Checks for the existence or nonexistence of a file Works only with file names - not directory names Ch 11 49
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 Ch 11 50
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 Ch 11 51
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 Ch 11 52
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 Ch 11 53
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 Ch 11 54
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 Ch 11 55
The IF ERRORLEVEL Command Testing Program can set an exit code when it finishes executing. Batch file tests exit code with IF ERRORLEVEL statement. Ch 11 56
The IF ERRORLEVEL Command Testing An exit code is tested with ERRORLEVEL to determine if it is greater than or equal to it. Ch 11 57
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. Ch 11 58
Activity Using IF ERRORLEVEL with COPY KEY CONCEPT: User exit codes successfully in a batch file Ch 11 59
Writing Programs to Test for Key Codes Exit codes: Set by an operating system program Created by writing small program based upon an activity Ch 11 60
Writing Programs to Test for Key Codes Every time a key is pressed, it is identified by a scan code. Ch 11 61
Writing Programs to Test for Key Codes Can write programs by using: Programming language DEBUG (operating system utility program) Ch 11 62
Writing Programs to Test for Key Codes Easiest way to use DEBUG is to create a script file. Ch 11 63
Writing Programs to Test for Key Codes A script file is set of instructions that can be written in any text editor. Ch 11 64
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. Ch 11 65
Writing Programs to Test for Key Codes Summary of command available assemble compare dump enter fill go hex input load move name output within BUG program (p. 576) A (Address) C range address D (range) E address (list) F range list G [=address] [addresses] H value1 value2 I port L [address] [drive] [firstsector] [number] M range address N [pathname] [arglist] O port byte Ch 11 66
Writing Programs to Test for Key Codes Summary of command available proceed quit register search trace unassemble write within BUG program (p. 576) P [=address][number] Q R [register] S range list T [=address] [value] U [range] W {address] [drive] [firstsector] [number] allocate expanded memory deallocate expanded memory map expanded memory pages display expanded memory status XA [#pages] XD [handle] XM [Lpage] [Ppage] [handle] XS Ch 11 67
Writing Programs to Test for 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 68
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 Ch 11 69
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 Ch 11 70
The Environment The environment is an area in memory where data can be stored. Ch 11 71
The Environment Data: Constant/fixed values - never change Variables that do change - depends on conditions or information passed to program Ch 11 72
The Environment In programming, an expression is any legal combination of symbols that represent a value. Ch 11 73
The Environment The operating system stores data in form of two strings: Name of variable Value of variable Ch 11 74
The Environment Environmental variable is name assigned to string (value) of data. Ch 11 75
The Environment User can set environmental variables. Some common variables set when Windows started. Ch 11 76
The Environment User can leave messages in environment using SET command. Ch 11 77
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. Ch 11 78
The Environment SET syntax: SET [ variable = [string] ] Ch 11 79
The Environment SET without parameters displays current environmental variables. Ch 11 80
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 Ch 11 81
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 Ch 11 82
Using SET and the Environment in Batch Files Can use built-in environmental variables that Windows sets and uses. Can set own environmental variables. Ch 11 83
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 Ch 11 84
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 Ch 11 85
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. Ch 11 86
Activity Using DIRCMD KEY CONCEPTS: Can determine how to display batch files Files displayed this way until change values or close Command Prompt session Ch 11 87
FOR IN DO Command FOR..IN..DO command: Allows repetitive processing Can be issued at the command line placed in a batch file Ch 11 88
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. Ch 11 89
FOR IN DO Command Syntax at command line: FOR %variable IN (set) DO command [command-parameters] Ch 11 90
FOR IN DO Command Syntax in a batch program: FOR %%variable IN (set) DO command [command-parameters] Ch 11 91
FOR IN DO Command GOTO loop is vertical. FOR IN DO loop is horizontal. Ch 11 92
FOR IN DO Command There is a difference between a variable and a parameter. Ch 11 93
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 Ch 11 94
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 Ch 11 95
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) Ch 11 96
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 Ch 11 97
Activity Using Additional Features of FOR IN DO Command 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 Ch 11 98
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 Ch 11 99
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 Ch 11 100
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 Ch 11 101
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> Ch 11 102