Batch Files

Batch Files

Sections: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Index | Next >

IF Command (Internal)

Learning objective: (2) Explain the use of the IF command in batch files


Click on image to enlarge.

The IF command gives power to the execution of a batch file. It allows the batch file to have multiple outcomes depending on conditions it encounters during its execution. There are three different tests the IF command can test for. It can test to see if two strings of text are equal, it can test to see if a file exists, and it can test for a error code when an application ends. The the error code for normal termination of an application is zero. If another code is generated, you can consult with the application documentation for what the error code means.

IF "text1" == "text2" GOTO label

The IF command can test to see if two sets of characters or strings are equal. If they are, a GOTO command can be used to jump to another location in the batch file for processing of this event.

IF [NOT] EXIST filename GOTO label

The IF command can test to see if a file exists. If it exists or does not exist, a GOTO command can be used to jump to another location in the batch file for processing of this event.

IF [NOT] ERRORLEVEL n GOTO label

The IF command can test the ERRORLEVEL of an application. The default error level is 0 which signifies there was no errors in the processing of the application. If another error level is detected, a GOTO command can be used to jump to another location in the batch file for processing of this event. You will need to consult vendor documentation to view their codes for their application as to what they mean. For example, maybe the log file was not printed because of a paper jam. The application processes the job, like payroll, but there was no paper audit of the transactions. The vendor could make this an error level of 12. Thus, if an error code of 12 was detected, a message could be sent to the console operator for further action.

Thinking: What does the IF command bring to the batch files?

Key terms: ERRORLEVEL, EXIST, IF, strings

Resources:
To maximize your learning, please visit these Web sites and review their content to help reinforce the concepts presented in this section.

Quick links:
IF @ ss64.com
IF and GOTO command @ web.csulb.edu
How to Avoid Spaghetti Code @ www.robvanderwoude.com

Embedded Resources

Notes on navigation: Click inside the frame to navigate the embedded Web page. - Click outside the frame to navigate this page to scroll up/down between the embedded Web pages. - Click on the frame title to open that page in a new tab in most browsers. - Click on the the "Reload page" link to reload the original page for that frame.

IF @ ss64.com | Reload page | If frame is empty, click on the link to view the page in a new tab or window

IF and GOTO command @ web.csulb.edu | Reload page | If frame is empty, click on the link to view the page in a new tab or window

How to Avoid Spaghetti Code @ www.robvanderwoude.com | Reload page | If frame is empty, click on the link to view the page in a new tab or window

Sections: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Index | Next >