Batch processing command instruction if statement _dos/bat

Source: Internet
Author: User
Tags echo d

If is used for conditional judgment, it applies to the following scenarios:

1, to determine whether the drive, file or folder exists, with if exist statement;
2, to determine whether the two strings are equal, with the If "string 1" = "string 2" statement;
3, to determine whether the two values are equal, using the IF value 1 equ value 2 statements;
4, to determine whether a variable has been assigned value, using if defined STR statement;

The full format of the IF statement is this: if conditional expression (statement 1) Else (statement 2), which means that if the conditional expression is set up, then the statement 1 is executed, otherwise the statement 2 is executed.

For each of the above four scenarios, you can use the following code:

1. If exist D:\test.txt (Test.txt exists under echo D disk) Else (test.txt not present under Echo D disk)
2, if "abc" = = "xyz" (Echo string ABC equals string xyz) Else (Echo string ABC is not equal to string xyz)
3, if 1 equ 2 (echo 1 equals 2) Else (echo 1 is not equal to 2)
4, if defined str (echo variable str has been assigned a value of%str%) else (the value of echo variable str is null)

When judging whether strings are equal, if the case is case-sensitive, for example, the simple if statement will assume that the string ABC is not the same as the string ABC, and if you do not want to be case-sensitive, you need to add the/I switch, using the if/i "string 1" = "string 2" format; Rather than a separate "=".

To determine the size relationship between two values, in addition to being equal to equ, there are other relationship symbols, and all the relationship symbols that apply to if statements are shown in the following table:

Chinese meaning

Relationship characters

English explanation

Equals

Equ

Equal

Greater than

Gtr

Greater than

Greater than or equal to

Geq

Greater than or equal

Less than

Lss

Less than

Less than or not equal to

Leq

Less than or equal

Not equal to

Neq

No equal

The IF statement also has a thin format: an If conditional expression statement, which means that if a conditional expression is set up, the statement is executed, otherwise, nothing is done.

Examples of multiple-layer if nested syntax:

If exist a.txt (
  if exist 1.txt (
    echo exists a.txt and 1.txt
  ) Else (
    Echo exists a.txt but does not exist 1. txt
  )
) Els E if exist b.txt (
  echo does not exist a.txt but exists b.txt
) Else (
  Echo does not exist a.txt and b.txt
)

The above mentioned is the entire content of this article, I hope you can enjoy.

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.