Older Operating Systems
Total Page:16
File Type:pdf, Size:1020Kb
Older Operating Systems Class Notes # 10 Batch Files (Part 2) October 23, 2003 IF Condition When a given condition is true, the statement is executed. When the given condition is false, the statement is not executed. Figure one illustrates the flow chart for the IF condition. IF entry point No Decision Yes Statement (perform some action) IF exit point Figure 1: IF condition flow chart The IF condition tests the following three conditions: • The ERRORLEVEL of a program • Whether a string is equal to another string • Whether a file exists Using IF to test ERRORLEVEL The syntax for testing the ERRORLEVEL is: IF ERRORLEVEL number command This form of the IF condition determines whether the value of ERRORLEVEL is greater than or equal to a number specified in the number parameter. IF NOT ERRORLEVEL number command This form of the IF condition determines whether the value of ERRORLEVEL is not greater than or equal to a number specified in the number parameter. 1 If the specified condition is true, DOS executes the command specified in the command parameter. Otherwise, DOS skips to the next line in the batch file without executing the command in the command parameter. The only DOS commands that leave an ERRORLEVEL (exit) code are: BACKUP, DISKCOMP, DISKCOPY, FORMAT, GRAFTABL, KEYB, REPLACE, RESTORE, and XCOPY. Many other programs generate exit codes, however. For example, table one contains codes that are generated by the DISKCOPY command. Table one: DISKCOPY codes Code Description 0 The operation was successful. 1 A read/write error occurred that did not terminate the disk copy operation. 2 The user pressed Ctrl-C. 3 A fatal read/write error occurred and terminated the copy procedure before it was completed. 4 An initialization error occurred. An IF condition in a batch file allows you to test for the code generated by a DOS command or program to determine whether the command or program was successful. Example: Figure two is a batch file named DCOPY.BAT that makes disk copies in your drive A. If the disk copy procedure terminates before completion, the batch file will inform you of the cause. @ECHO OFF DISKCOPY A: A: /V IF ERRORLEVEL 4 GOTO INIT_ERR IF ERRORLEVEL 3 GOTO FATL_ERR IF ERRORLEVEL 2 GOTO CTRL_C IF ERRORLEVEL 1 GOTO NON_FATL ECHO DISKCOPY successful and verified! GOTO END :INIT_ERR ECHO Initialization error! GOTO END :FATL_ERR ECHO Fatal error! DISKCOPY stopped! GOTO END :CTRL_C ECHO Someone pressed Ctrl-C! GOTO END :NON_FATL ECHO A non-fatal error occurred, check data! :END Figure 2: DCOPY.BAT batch file 2 To run this batch file, type DCOPY at the command line, and then press enter. DOS displays the following message: Insert SOURCE diskette in drive A: Press any key to continue_ When you press a key, DOS begins the disk copy procedure. After the DISKCOPY command in the batch file executes, the batch file run through a series of IF ERRORLEVEL tests. These tests are in descending order (4 to 1) because ERRORLEVEL considers any number equal to or greater than the specified number to be a match. Therefore, if you were to check for 1 first, 4 would also be a match, and you would never get to the proper test. Using IF to compare strings The second use for the IF condition is to test whether string1 equals string2. The syntax of the batch command is: IF string1 = string2 command If the two strings are identical, the condition is true and DOS executes the command specified in the command parameter. Otherwise, DOS skips to the next line in the batch file without executing the command in the command parameter. IF NOT string1 = string2 command By adding NOT to the IF condition, you can test for the condition when the two strings are not the same. Using IF to look for files The third type of IF condition tests whether a given file is on disk. The syntax for this form is: IF EXIST filename command This form of the IF command determines whether the file specified in the filename parameter exists on your computer’s disk. If the file does exist, the IF condition executes the command specified in the command parameter. IF NOT EXIST filename command By adding NOT to the IF condition, you can test for the condition when the file does not exist. Figure three illustrates an example of a batch file that tests for the existence of a file and deletes it if it exists. 3 @ECHO OFF CLS IF EXIST temp.txt DEL temp.txt Figure 3: Batch file that tests for a file and erase the file if it exists Pausing for Input You can temporarily halt the execution of a batch file and accept limited user input. You can use this feature to decide whether to process certain commands, to branch to a different part of a batch file, or even to present a menu and accept any of a series of choices. To do this, we use the CHOICE command. The syntax is: CHOICE /C:choices /N /S /T:c,nn message Table 2 Parameters Description /C:choices Lists the keys that can be pressed. If you don’t specify choices, the default is YN /N Prevents the display of acceptable keys at the end of the prompt. /S Instructs CHOICE to pay attention to the case of the keys pressed; this feature enables you to use Y and y for different choices. /T:c,nn Causes CHOICE to act as though you pressed the key represented by c if you don’t make a choice within nn seconds. Message Is the optional prompt to display. An example of a batch file using CHOICE is shown in figure four. @ECHO OFF CLS CHOICE /C:1234 /N /T:Q,10 Press a number between 1 to 3, 4 to quit> IF ERRORLEVEL 4 GOTO END IF ERRORLEVEL 3 GOTO THREE IF ERRORLEVEL 2 GOTO TWO IF ERRORLEVEL 1 GOTO ONE :THREE ECHO You have pressed the number three GOTO END :TWO: ECHO You have pressed the number two GOTO END :ONE: ECHO You have pressed the number one :END ECHO Good-bye! Figure 4: An example of a batch file using CHOICE In this example, the user has to select a number between one to three on the keyboard. To quit, the user enters the number four. Also, if the user does not make a selection within 10 seconds, the batch file will terminate. 4.