Dos/bat batch processing if exist else statement for several uses

Source: Internet
Author: User

In DOS batch commands are often judged by the IF statement to execute the following command, then how to use the batch if statement, the following worry-free small to say about the batch if and if exist else statement related content.

batch processing if writing format
If condition expression (statement 1) Else (statement 2), it means that if the conditional expression is true, then statement 1 is executed, or statement 2 is executed.
two or four ways to use batch if
1. Determine if the drive, file or folder exists, using the IF exist statement
For example: if exist c:\xue51com.txt (there is xue51com.txt presence under the Echo C disk) Else (Xue51com.txt is not present under the Echo C drive)

2. Determine if two strings are equal, using the If "string 1" = = "String 2" statement
For example: if "xue" = = "xyz" (Echo string xue equals string xyz) Else (Echo string xue is not equal to string xyz), this example is mainly the detection of strings (note that when using if to detect the equality of strings, it is later used instead of "=", but "=") ”)

3. Determine if two values are equal, use if value 1 equ value 2 statement
Example: if 1 equ 2 (echo 1 equals 2) Else (echo 1 is not equal to 2), this example function is to detect the value:
(Note that the greater than symbol in the batch cannot be used: ">", and "gtr", others are similar)
Comparison operators at a glance:
EQU-equals
NEQ-Not equal to
LSS-Less than
LEQ-less than or equal to
GTR-Greater than
GEQ-greater than or equal to

4. Determine if a variable has been assigned, using the IF defined STR statement
Example: if defined str (echo variable STR is already assigned with a value of%str%) else (the value of the echo variable str is null)

5, if combined with errorlevel use
Description: The initial value of the environment variable errorlevel is 0, and when some command execution is unsuccessful, a value is returned, such as: 1, 2, etc.
If-errorlevel
Example:
XCOPY F:\test.bat D:\
IF ERRORLEVEL 1 ECHO file copy failed
IF ERRORLEVEL 0 ECHO Successful copy file
If the file copy succeeds, the screen will display "Copy file successfully" or the "copy of file failed" will be displayed.
IF ERRORLEVEL is used to test the return value of its previous DOS command, note that it is only the return value of the previous command, and the return value must be judged in order from large to small.
Therefore, the following batch file is incorrect:
@ECHO OFF
XCOPY C:\AUTOEXEC. BAT D:\
IF ERRORLEVEL 0 ECHO Successful copy file
IF ERRORLEVEL 1 ECHO not found copy file
IF ERRORLEVEL 2 ECHO user aborts copy operation via Ctrl-c
IF ERRORLEVEL 3 ECHO Preset error prevents file copy operation
IF ERRORLEVEL 4 Write error during ECHO copy
Whether the copy is successful or not, the following:
Copy file not found
User aborts copy operation via Ctrl-c
Preset error prevents file copy operation
Write error in copy process
These are some of the uses of Os/bat batch if, and hope to help you.

Dos/bat batch processing if exist else statement for several uses

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.