1.18k likes | 1.44k Views
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.
E N D
Chapter 11 Advanced Batch Files Ch 11
Overview Quick review of batch file commands learned in earlier chapters. Ch 11
Overview Advanced features of these commands will be explained and used. Ch 11
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
Overview Will refine techniques in working with environment. Ch 11
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
Batch File Commands Any command used at the command line can be used in a batch file. Ch 11
Batch File CommandsTable 11.1 Batch File Commands p. 548 Ch 11
Batch File CommandsTable 11.1 Batch File Commands p. 548 Ch 11
Batch File Commands Batch files have: • Limited vocabulary (commands) • Syntax • Programming logic Ch 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 Ch 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
Review of REM, PAUSE, and ECHO Commands To interrupt a batch file during execution: • Press <Ctrl> + C • Press <Ctrl> + <Break> Ch 11
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
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
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
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
Advanced Features of ECHO and REM Using NUL will not suppress a message such as “file not found”. Ch 11
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
Advanced Features of ECHO and REM To insert a blank line, key in ECHO followed by a period (ECHO.) Ch 11
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
The GOTO Command GOTO command: • In conjunction with a label creates a loop • Processes command following label Ch 11
The GOTO Command Loop repeats steps until stopped by . . . • using an IF statement. • breaking into the batch file with <Ctrl> + C. Ch 11
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
The GOTO Command GOTO has one parameter: • GOTO label Ch 11
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
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
The SHIFT Command SHIFT command allows for an unlimited number of parameters on the command line. Ch 11
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
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
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
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
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
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
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
The IF Command Using Strings • IF can be used to compare strings. • Two equal signs (= = ) separate items to be compared. Ch 11
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
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
Testing for NULL Values If SHIFT used in a batch file will be caught in endless loop when all parameters are used. Ch 11
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
Testing for NULL Values Can test for a NULL value using IF with quotation marks. IF “%1” = = GOTO LABEL Ch 11
Testing for NULL Values Can test for a NULL value using IF with any word. IF %1word = = word GOTO LABEL word==word Ch 11
Testing for NULL Values Can test for a NULL value using IF with backslash. IF \%1\= =\\ GOTO LABEL \\==\\ Ch 11
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
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 Ch 11
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