DOS If condition judgment _dos/bat

Source: Internet
Author: User
Tags echo d

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 relation character English explanation

Equal to 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 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.

Learn if conditional statements

Learning points:
Basic syntax for 5 if statements:
1. Determines whether two strings are equal, if "string 1" = "String 2" command statement;
2. Determine whether two values are equal, if value 1 equ value 2 command statement;
3. Judge whether the drive, file or folder exists, if exist filename command statement;
4. Determine if the variable is already defined, if defined variable command statement;
5. Determines the return value of the previous command, if errorlevel a numeric command statement.
------------------------------------------------------------------------------------------------------


1. To determine whether two strings are equal, if "string 1" = "String 2" command statement
Note: In "string 1" = "String 2", there are two consecutive "="

Example 1

@echo off
set/p var1= Enter the first character to compare:
set/p var2= Please enter a second softer character:
if "%var1%" = = "%var2%" (two characters of echo input) else echo Two characters entered are not the same
pause

The execution will require you to enter two strings, and then the batch process determines whether they are the same. If the string is judged to be equal, if it is case-sensitive
, please see,

Example 2

@echo off
If "a" = = "a" (echo input two characters same) else Echo entered two characters different
pause

Execution will show: two characters entered are not the same
If we don't want it to be case-sensitive, you can add the/I parameter. Looking at the following example,

Example 3

@echo off
if/i "a" = = "A" (two characters of echo input) else Echo's two characters are not the same
pause

This execution will show: The two characters entered are the same
------------------------------------------------------------------------------------------------------


2. Determine if two values are equal, if value 1 equ value 2 Command statement
The equ in the statement represents the meaning of equality, and the size relationship between the two values also has the following relationship symbols:

Chinese meaning relation character English explanation
Equal to 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 equal to leq less than or equal
Not equal to NEQ no equal

Let's take a look at an example

Example 4

@echo off 
set/p var= Please enter a number: 
if%var% Geq (Echo is greater than or equal to) else echo this number is less than ten
pause

Note that, in batch processing, the greater than, less than, equals number, and so on can not be used: ">" "<" "=" these symbols, but to use like "gtr" such
Of


☆ Note: If comparison between strings and comparison numbers, their difference is reflected in the quotation marks "" above, see the following example

"Example 1"

@echo off
If "LSS" "4" (Echo 12 is less than 4 oh?) Else echo 12 of course not less than 4!
Pause

The result: 12 is less than 4 oh?


"Example 2"

@echo off
if LSS 4 (Echo 12 is less than 4 oh?) Else echo 12 of course not less than 4!
Pause

The result of the execution is: 12 of course not less than 4!

Why does "Example 1" add double quotes "" Can make a mistake?

The reasons are as follows:
If you want to compare the two elements with double quotes "", then it will be treated as a character comparison. Two elements to compare the process is: first compare two elements
The first, if the first is the same, and then compare the second, if the second one is the same, then compare the third place ... Accordingly, in "Example 1" if "LSS"
In the "4" statement, the essence is 1 and 4 comparison, 1 of course less than 4, so executed the following command: Echo 12 unexpectedly less than 4 oh?

☆ Suggest: If it is a string comparison use double quotes "" is the comparison of numbers without double quotes!
------------------------------------------------------------------------------------------------------


3. Judge whether a drive, file or folder exists, if exist filename command statement
Let's take a look at the statement that determines whether a drive, file, or folder exists, where if exist filename is represented: the existence of the filename
Mean.

Example 5

@echo off 
if exist "E:" (presence of echo e disk) Else echo e disk does not exist 
Pause>nul

Here is the Judge E: whether there is!

Example 6

@echo off
if exist D:\123.bat (echo 123.bat file exists!) else echo 123.bat file does not exist! 
Pause

This example is used to determine whether 123.bat files exist, but not rigorous! If 123.bat is a folder instead of a file, the above sentence
Cut it! So how do you determine if the specified file 123.txt exists? Please see:

@echo off
dir/a-d d:\123.bat >nul 2>nul
if%errorlevel%==0 (echo 123.bat file exists!) else echo 123.bat file does not exist! 
Pause

Use Dir's/a-d parameter to remove the 123.bat directory attribute, specifying that the 123.bat to search is a file instead of a folder, and that the result (including the correct
and error) shielding (>nul 2>nul), if Dir finds the file 123.bat, then its ERRORLEVEL value (the exit encoding of the DIR command) is set to 0, no
is 1, then there is no such file. Of course you can also use | | and && to judge. about why you should use if%errorlevel%==0 instead of if errorlevel 0
It? There will be a description in the following if errorlevel!

Example 7

@echo off
if exist test\ (echo test is a folder) else echo test is a file
pause

This is to determine if the folder exists. We can not use the following two ways to determine the existence of a folder:

The first ① species

@echo off
if exist test\. (Echo test is a folder) else echo test is a file
pause


The first ② species

@echo off
if exist Test\nul (echo test is a folder) else echo test is a file
pause


4. Determine if the variable is already defined, if defined variable command statement
This is a statement that determines whether a variable has been defined, but let's look at the example first.

Example 8

@echo off
If defined a (echo variable A is defined) else (echo variable A is not defined)
pause

Show after execution: variable A is not defined

Example 9

@echo off
set a=
if defined a (definition of echo variable a) else (echo variable A is not defined)
pause

Show after execution: variable A is not defined

Note: The set a= has no spaces behind it.

Example 10

@echo off
set a=10
if defined a (definition of echo variable a) else (echo variable A is not defined)
pause

Show after execution: variable A is defined

Look at the above three examples, you should find something? Now we should know: when the variable does not exist or the value is empty, the variable is undecided
Yi.
When we use the IF defined variable command statement to determine whether a variable is defined, note that the variable is a variable name that does not use the guide symbol%, and does not
Can be written as% variable%, otherwise error.

Let's try these two examples below,

Example 11

@echo off
set var1=5
if defined var1 (echo variable var1 defined) Else (echo variable var1 not defined) set/p var2=
Please enter a number:
if Defi Ned%var2% (echo variable var2 defined) Else (echo variable var2 not defined)
pause

Example 12

@echo off
set var1=5
if defined var1 (echo variable var1 defined) Else (echo variable var1 not defined) set/p var2=
Please enter a number:
if def ined var2 (echo variable var2 defined) Else (echo variable var2 not defined)
pause

Note Example 11 and example 12 OH: One is%var2% and the other is var2.


5. To determine the back value of the last command, if errorlevel the value command statement
This statement is used to determine the return value of the previous command execution errorlevel, let's take a look at the example first.

Example 13

@echo off 
net user 
if%errorlevel% = 0 (Successful execution of the Echo Net user command) else (the Echo net user command fails to execute) 
Pause

Note:%errorlevel% This is a system variable, so enclose it in two%, here = = two consecutive =
Maybe some of my friends said that my example was wrong? According to the grammar should be written "if errorlevel 0" is right.
If that's how you use it, you're wrong, and you don't believe it? OK, let's design an experiment to see

Example 14

@echo off 
set/p input= Please enter any command: 
if errorlevel 0 (Echo%input% command execution succeeded) Else (Echo%input% command execution failed)
pause

With this syntax, regardless of the previous command, whether the execution succeeds, it will assume that the command succeeded. What's the solution?


The characteristics of an if ERRORLEVEL statement:
When using the IF errorlevel 0 cmmand sentence, it means that if the error code value returned is greater than or equal to 0, the Cmmand
Operation
When using an if%errorlevel%==0 cmmand sentence, it means that the Cmmand action will be performed if the error code value that is returned equals 0.

In general, the result of the previous command returned only two values, "success" in 0 means "failed" in 1, in effect, the ERRORLEVEL return value can be
Between the 0~255,
For example, Xcopy defaults to a ERRORLEVEL value of 5, representing 5 execution states respectively:

0 Copy file Success
1 copy File not found
2 user terminated xcopy operation via CTRL C
4 initialization error occurred
5 A disk write error has occurred


For if conditional statements, commonly used in the above 5 bar, let's take a look at its two forms:

The full format of the IF condition statement is: If condition expression (statement 1) Else (statement 2)
The implication is that if the conditional expression is set up, the statement 1 is executed, or the statement 2 is executed. Else Statement 2 can also be used without parentheses
Up

The simple format of the IF condition statement is: If condition expression (statement)
It means that if the conditional expression is set up, the statement is executed, otherwise nothing is done. The statement after the conditional expression can also be used without parentheses
Up

Example 15

@echo off 
if exist "D:\123.txt" (del "D:\123.txt")
pause

You can also write the following

Example 16

@echo off 
if exist "D:\123.txt" del "D:\123.txt"
Pause

Supplemental 1:if NOT statement
For the 5 basic syntaxes preceding the IF statement, you can add the not argument, such as
1. If not "string 1" = = "String 2" command statement;
2. If not value 1 equ value 2 command statement;
3. If not exist filename command statement;
4. If not defined variable command statement;
5. If not errorlevel numeric command statement.

In an IF condition expression (statement) statement, such as without the not parameter, the default is to determine when the condition is set, execute the statement;
When you fail, execute the statement.

Let's take a look at the following example:

Example 17

@echo off
if exist "C:\" (Echo C disk exists) Else echo C disk does not exist
pause

Show after execution: C disk exists

Example 18

@echo off
if not exist "C:\" (Echo C disk exists) Else echo C disk does not exist
pause

Show after execution: C disk does not exist

No, example 18 after the implementation of the show: C disk does not exist, this simple value is in a lie, that is what is going on?

If not exist "C:\" (Echo C disk exists) Else echo C disk does not exist
If there is no c:\ The execution shows that the C-disk exists; otherwise < refers to the presence of the c:\> on the execution of the display C disk does not exist.
Note: If exist means if it exists, then if not exist is if not present

Your C disk Of course is real, the root according to the Chinese in the above learned that the executive example 18 after the show as: C disk does not exist through the analysis of example 18, you
You should understand how the not parameter is used in the IF statement!


To complement the 2:IF statement nesting, let's take a look at

Example 1

@echo off
set a=55
if%a% geq (
  if%a% geq (if
    %a% geq (
      echo A is greater than or equal 
    ) else echo A is less than 1 
  else echo A is less than 
) else echo A is less than 
pause

This is a comparison of the standard of writing, then how is this written? So let's go one step at a moment:

First step:

If%a% Geq () else echo A is less than 50

The second step: in the first step Geq 50 after the parentheses () in the two press return, mainly empty two lines out.

If%a% Geq ( 
  if%a% geq () else echo less than
) else echo A is less than 50

Step three: In the second step Geq 80 after the parentheses () in the two press return, mainly empty two lines out.

If%a% geq (if 
  %a% geq ( 
   if%a% geq () else echo A is less than
  ) else echo less than
50 else echo A is less than

Fourth step: In the third step Geq 100 after the parentheses () in the two press return, mainly empty two lines out.

If%a% geq (if 
  %a% geq ( 
   if%a% geq (
     echo A is greater than or equal
   ) else echo a
  is less than 80 c15/>) Else echo A is less than 50

The above code, if%a% and) else should be indented appropriately to achieve a clear write book!

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.