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

IF WINDOWS<h1><a name="WINDOWS_ECHO_Strings_equal_pre_p_The_result_would_be_a_i_Strings_equal_i_message_p_p_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_p_p_The_IF_statement_s_optional_NOT_operator_provides_another_way_to_handle_string_comparisons_For_example_consider_the_following_command_p_pre_IF_NOT_WINDOWS">WINDOWS ECHO Strings equal</a></h1>

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.

Discuss this Article 1

Yordano (not verified)
on Apr 21, 2004
this article helped me to understand in a while the if statement of batch files. I visited other pages and I found nothing.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottYou'll have the opportunity to experience:
• The Microsoft
Technology Roadmap
• Office 365 Implementation
• Hyper-V Optimizing
• Windows 8 Deployment
and much more!

Come See Paul Thurrott & Rod Trent in Person!

Early Registration Now Open

Upcoming Training

Mastering System Center 2012

During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.