Bat Tutorial: Section 6 (if Command)

Source: Internet
Author: User

Recently, I found that some of my friends asked me how to use the if command. , The IF command is simple. , So I never mentioned it in the newbie teaching post. , Now let me add one article , I hope you can "Very bat" New friends help .
Start now :
Use if / ? Open if System Help ( Not all of them are listed by myself. ) ,
If [ Not ] Errorlevel number command
If [ Not ] String1 = = String2 command
If [ Not ] Exist filename command
First, the first usage If [ Not ] Errorlevel number command
The basic usage is to judge the previous Command Execution result Code , To determine the next step .
Generally, the execution result code of the previous command has only two results. , "Successful" Expressed as 0 "Failed" 1 .
For example:
@ Echo off
Net user
If % Errorlevel % = = 0 echo net user
Pause
This is a simple way to determine whether the previous command is successfully executed. .
Careful friends may find,This usage is not the same as that in help,Follow the instructions in help"If % errorlevel % = 0 echo net user execution successful! "This code is written:IfErrorlevel 0 echo net user
So why should I write it like this??After you change the code,You will find the error! Use this syntax,Whether or not your command is successfully executed,He thinks the command is successful.,I don't know if it is a bug or an error I understand....
% Errorlevel % This is a system variable. , "Successful" Expressed as 0 "Failed" 1 . Of course, there are other parameters. , These two numbers are basically used. .
Here are several examples for beginners to understand.
@ Echo off
Net usertest
If % Errorlevel % = = 1 echo net user
Pause
This indicates whether the previous command failed to be executed.
@ Echo off
Set / P VaR = Enter any command :
% VaR%
If % Errorlevel % = = 0 Goto Yes
Goto No
: Yes
Echo ! Execution successful
Pause
Exit
: No
Echo basically failed.. .
Pause
This is based on the command you entered ,
In a simplified version
@ Echo off
Set / P VaR = Enter any command :
% VaR %
If % Errorlevel % = = 0 ( Echo % VaR % Execution successful ) Else Echo % VaR %
Pause

Of course, we can also split the if else statement into several lines to write it out. , Make him look nice . . .
@ Echo off
Set / P VaR = Enter any command :
% VaR %
If % Errorlevel % = = 0 (
Echo ! Execution successful
) Else (
Echo basically failed. . .
)
Pause
The two abbreviations described here can be applied to all the three if syntaxes. , Not only [ Not ] Errorlevel number command
This method can be used.
Now the second method is if [ Not ] String1 = = String2 command
This is used to compare whether the values of variables or characters are equal. .
Example
@ Echo off
Set / P VaR = Enter the first comparison character :
Set / P var2 = Enter the second comparison character :
If % VaR % = = % Var2 % ( ECHO we are equal ) Else ECHO we are not equal
Pause
In the preceding example, we can determine whether the input values are equal. , However, if you enter the same character , However, if one of them is followed by a space ,
In this example, the values are equal. , How can we make the input with spaces not equal?? We can add double quotation marks to the comparison characters. .
@ Echo off
Set / P VaR = Enter the first comparison character :
Set / P var2 = Enter the second comparison character ( Try inputting multiple Spaces ) :
If "% Var %" = = "% Var2 %" ( ECHO we are equal ) Else ECHO we are not equal
Pause
Third usage: If [ Not ] Exist filename command
This is to judge File Or whether the folder exists.
Example
@ Echo off
If Exist "C: \ test" ( ECHO file exists ) Else ECHO does not exist.
Pause
The file path to be determined is enclosed by quotation marks to prevent spaces in the path. ,
This syntax is rarely used. , That's all. , I will not discuss it much. .
In addition, we can see that each if statement has [ Not ] Statement , What does this mean?? Other words added , It indicates that when we judge that our conditions are not successful ,
If this parameter is not added, the system first determines when the condition is set. , For example
@ Echo off
If Not Exist "C: \ test" ( ECHO file exists ) Else ECHO does not exist.
Pause
Add a not , What are the results after execution? , If your C drive is not c : \ Test , It will still display "File exists" , This indicates that if not is added
The condition will fail to be judged first! Understand. ,
@ Echo off
If Not Exist "C: \ test" ( ECHO does not exist. ) Else ECHO file exists
Pause
In addition, if has some enhanced usage , As follows:
If [ / I ] String1 compare - OP string2 command
If Cmdextversion number command
If Defined variable command
Next two usage , I will not introduce it , Because they have basically the same meaning as the above usage, Just to put it simply If [ / I ] String1 compare - The OP string2 command statement is case insensitive when determining characters. , Let's look at the two examples.
@ Echo off
If A = = A ( ECHO we are equal ) Else ECHO we are not equal
Pause
After execution, we are not equal
@ Echo off
If / I = = A ( ECHO we are equal ) Else ECHO we are not equal
Pause
Add /
At the end, there are some symbols used to judge numbers.
Equ - Equal
NEQ - Not equal
LSS- Less
Leq - Less than or equal
Gtr - Greater
Geq - Greater than or equal
Let me give you an example. , Everyone understands mathematics. . . . No more
@ Echo off
Set / P VaR = Enter a number :
If % VaR % Leq 4 ( Echo I am less than or equal to 4 ) Else Echo I am not less than or equal to 4
Pause

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.