Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch 111 Advanced Batch Files. Ch 112 Overview Quick review of batch file commands learned in earlier chapters.

Similar presentations


Presentation on theme: "Ch 111 Advanced Batch Files. Ch 112 Overview Quick review of batch file commands learned in earlier chapters."— Presentation transcript:

1 Ch 111 Advanced Batch Files

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

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

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

5 Ch 115 Overview Will refine techniques in working with environment.

6 Ch 116 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 Ch 117 Batch File Commands Any command used at the command line can be used in a batch file.

8 Ch 118 Batch File Commands Table 11.1 Batch File Commands p. 548

9 Ch 119 Batch File Commands Table 11.1 Batch File Commands p. 548

10 Ch 1110 Batch File Commands Batch files have: è Limited vocabulary (commands) è Syntax è Programming logic

11 Ch 1111 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 Ch 1112 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 Ch 1113 Review of REM, PAUSE, and ECHO Commands To interrupt a batch file during execution: è Press + C è Press +

14 Ch 1114 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 Ch 1115 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 and “ECHO OFF” will not appear on screen.

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

17 Ch 1117 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 Ch 1118 Advanced Features of ECHO and REM Using NUL will not suppress a message such as “file not found”.

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

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

21 Ch 1121 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

22 Ch 1122 The GOTO Command GOTO command: è In conjunction with a label creates a loop è Processes command following label

23 Ch 1123 The GOTO Command Loop repeats steps until stopped by... è using an IF statement. è breaking into the batch file with + C.

24 Ch 1124 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 Ch 1125 The GOTO Command GOTO has one parameter: è GOTO label

26 Ch 1126 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

27 Ch 1127 Activity—Using the GOTO Command Example of a Batch file to delete all files from many floppy 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

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

29 Ch 1129 Activity—Using the Shift Command

30 Ch 1130 Activity—Using the Shift Command

31 Ch 1131 Activity—Using the Shift Command

32 Ch 1132 Activity—Using the Shift Command

33 Ch 1133 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

34 Ch 1134 Activity—Using the Shift Command KEY CONCEPTS: è + sign tells OS to concatenate files è Contents of file ended when see EOF åTypically + Z åCOPY command places second + Z at end of file creates problem åSolve by copying file in binary mode

35 Ch 1135 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. è + C used to “break out”

36 Ch 1136 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 Ch 1137 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 Ch 1138 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 Ch 1139 The IF Command IF command syntax : IF è IF [NOT] ERRORLEVEL number command è IF [NOT] string1==string2 command è IF [NOT] EXIST filename command è Note: complete syntax in Appendix H

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

41 Ch 1141 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 Ch 1142 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

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

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

45 Ch 1145 Testing for NULL Values Can test for a NULL value using IF with quotation marks. IF “%1” = = GOTO LABEL

46 Ch 1146 Testing for NULL Values Can test for a NULL value using IF with any word. IF %1word = = word GOTO LABEL word==word

47 Ch 1147 Testing for NULL Values Can test for a NULL value using IF with backslash. IF \%1\= =\\ GOTO LABEL \\==\\

48 Ch 1148 Activity—Using NULL Values KEY CONCEPTS: è Test for a null value using quotation marks è Test for null value using a user designated word

49 Ch 1149 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 Ch 1150 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 Ch 1151 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 Ch 1152 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

53 Ch 1153 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

54 Ch 1154 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

55 Ch 1155 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

56 Ch 1156 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 Ch 1157 The IF ERRORLEVEL Command Testing An exit code is tested with ERRORLEVEL to determine if it is greater than or equal to it.

58 Ch 1158 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 Ch 1159 Activity—Using IF ERRORLEVEL with COPY KEY CONCEPT: è User exit codes successfully in a batch file

60 Ch 1160 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 Ch 1161 Writing Programs to Test for Key Codes Every time a key is pressed, it is identified by a scan code.

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

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

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

65 Ch 1165 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 Ch 1166 Writing Programs to Test for Key Codes Summary of command available within BUG program (p. 576) assembleA (Address) compareC range address dumpD (range) enterE address (list) fillF range list goG [=address] [addresses] hexH value1 value2 inputI port loadL [address] [drive] [firstsector] [number] moveM range address nameN [pathname] [arglist] outputO port byte

67 Ch 1167 Writing Programs to Test for Key Codes Summary of command available within BUG program (p. 576) proceedP [=address][number] quitQ registerR [register] searchS range list traceT [=address] [value] unassembleU [range] writeW {address] [drive] [firstsector] [number] allocate expanded memoryXA [#pages] deallocate expanded memoryXD [handle] map expanded memory pagesXM [Lpage] [Ppage] [handle] display expanded memory statusXS

68 Ch 1168 Writing Programs to Test for Key Codes.COM Program Written with DEBUG to return scan code of Pressed Key A:\>DEBUG -a E:0100 mov ah,8 158E:0102 int E:0104 cmp al,0 158E:0106 jnz 10a 158E:0108 mov ah,8int21 158E:010A mov ah,4c 158E:010C int E:010E mov ah,8 -r cx CX 0000 :e -n reply.com -w Writing 000E bytes -q

69 Ch 1169 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 Ch 1170 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

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

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

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

74 Ch 1174 The Environment The operating system stores data in form of two strings: è Name of variable è Value of variable

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

76 Ch 1176 The Environment è User can set environmental variables. è Some common variables set when Windows started.

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

78 Ch 1178 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 Ch 1179 The Environment SET syntax: SET [ variable = [string] ]

80 Ch 1180 The Environment SET without parameters displays current environmental variables.

81 Ch 1181 Activity—Using SET and the Environmental Variables KEY CONCEPTS: è Command processor must be in memory when keying in command è Using MORE å Pressing - moves one line at a time å Pressing - goes to end of file è SET with letter of alphabet - environmental variables beginning with letter displayed

82 Ch 1182 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

83 Ch 1183 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 Ch 1184 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 Ch 1185 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

86 Ch 1186 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 Ch 1187 Activity—Using DIRCMD KEY CONCEPTS: è Can determine how to display batch files è Files displayed this way until change values or close Command Prompt session

88 Ch 1188 FOR…IN…DO Command FOR..IN..DO command: è Allows repetitive processing è Can be å issued at the command line å placed in a batch file

89 Ch 1189 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.

90 Ch 1190 FOR…IN…DO Command Syntax at command line: FOR %variable IN (set) DO command [command-parameters]

91 Ch 1191 FOR…IN…DO Command Syntax in a batch program: FOR %variable IN (set) DO command [command-parameters]

92 Ch 1192 FOR…IN…DO Command è GOTO loop is vertical. è FOR…IN…DO loop is horizontal.

93 Ch 1193 FOR…IN…DO Command There is a difference between a variable and a parameter.

94 Ch 1194 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

95 Ch 1195 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

96 Ch 1196 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)

97 Ch 1197 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

98 Ch 1198 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

99 Ch 1199 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

100 Ch 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 Ch Activity—Using CALL KEY CONCEPTS: è Use + L - to eject a page è Use + 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 Ch 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 Line 2: CD >> A:\BATCH\HOMESAVE.BAT Line 3:CALL HOMNESAVE.BAT Line 4: DEL A:\BATCH\HOMESAVE.BAT


Download ppt "Ch 111 Advanced Batch Files. Ch 112 Overview Quick review of batch file commands learned in earlier chapters."

Similar presentations


Ads by Google