DOS if condition judgment

Source: Internet
Author: User
Tags echo d

1. Use the if exist statement to determine whether a drive, file, or folder exists;
2. Determine whether two strings are equal. Use the if "string 1" = "string 2" statement;
3. Determine whether two values are equal. Use the if value 1 equ value 2 Statement;
4. Determine whether a variable has been assigned a value. Use the if defined STR statement;

The complete format of the IF statement is as follows: if condition expression (statement 1) else (Statement 2), which means that if the condition expression is true, Statement 1 is executed, otherwise, Statement 2 is executed.

For the above four cases, you can use the followingCode:

1. If exist D: \ test.txt (test.txt exists under ECHO d) else (test.txt exists under echo d)
2. If "ABC" = "XYZ" (echo string ABC is equal to string XYZ) else (echo string ABC is not equal to string XYZ)
3. If 1 equ 2 (Echo 1 is equal to 2) else (Echo 1 is not equal to 2)
4. If defined STR (the echo variable STR has been assigned a value and its value is % STR %) else (the echo variable STR value is blank)

When determining whether a string is equal, if is case sensitive. For example, a simple if Statement considers that the string ABC is different from the string ABC. If you do not want to distinguish the case from the string ABC, you need to add the/I switch, use the IF/I "string 1" = "string 2" format; in addition, equal to the symbol is continuous "=" rather than separate "= ".

To determine the relationship between two values, in addition to using equ, there are other relational symbols. The following table lists all the relational symbols applicable to the IF statement:

English description

Equal to equ equal
Greater than GTR greater
Greater than or equal to geq greater than or equal
Less than LSS less
Less than or equal to Leq less than or equal
Not equal to NEQ no equal

The IF statement also has a simplified format: If condition expression statement, which means that if the condition expression is true, the statement will be executed. Otherwise, nothing will be done.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.