Using Conditional Statements

Conditional statements are a feature of every programming language. Windows includes the MS-DOS IF statement, which you'll find useful in batch files and shell scripts. The functionality of the IF statement is fairly straightforward: The OS evaluates a specific condition, and according to the result of the evaluation, the OS performs specified actions.

Windows' IF statement uses the following syntax:

IF \[NOT\] <some condition> <action if condition is true>

For example, to compare two strings you would type


WINDOWS ECHO Strings equal

The result would be a Strings equal message.

Note that in IF statements, string comparisons are case-sensitive. In the previous example, changing one of the WINDOWS to Windows would cause the OS to evaluate the condition as false and ignore the action statement.

The IF statement's optional NOT operator provides another way to handle string comparisons. For example, consider the following command:

IF NOT WINDOWSwindows ECHO Strings not equal

In this example, WINDOWS (i.e., all capital letters) doesn't equal windows (i.e., all lowercase letters); therefore, the OS will display a Strings not equal message.

To use the IF statement to check whether a file exists, slightly change the basic syntax:

IF \[NOT\] EXIST <filename> <action if file exists>

For example, you might use

IF EXIST system.ini type system.ini

If system.ini exists, the OS will display the file's contents. Or, you might use

IF NOT EXIST system.ini type system.ini

If system.ini doesn't exist, the OS generates an error message.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.