BAT Batch If command example detailed _dos/bat

Source: Internet
Author: User
Examples of IF commands detailed

If, as it is in E, the meaning of "if" is used to make conditional judgments. If a condition is met, the following command is executed.
Used primarily to determine whether 1, two "strings" are equal, 2, two values are greater than, less than, equal, and then execute the appropriate command.
Of course there are special uses, such as combining errorlevel:if ERRORLEVEL 1 echo Error
or combine defined (definition meaning): If defined test (echo it is defined) else echo it isn ' t defined

Usage examples:
1, detection string (note, if the string is detected in the same time, the following is not "=", but "= ="):
Copy Code code as follows:

@echo off
Set "Str=this is a test"
REM detect variable%str% is equal to test, if equal, show OK, otherwise show no
If "%str%" = = "Test" (Echo OK) Else echo NO
Pause>nul

2, the detection value: (Note, batch processing is greater than the symbol can not be used: ">", and with "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
Detect if the value 20 is greater than 15 demo code:
Copy Code code as follows:

@echo off
set/a num1=20
set/a num2=15
If%num1% gtr%num2% echo%num1% is greater than%num2%
If%num1% EQU%num2% echo%num1% equals%num2%
If%num1% LSS%num2% echo%num1% less than%num2%
Pause>nul

3, if combined with ERRORLEVEL use:
Description: Environment variable ERRORLEVEL initial value is 0, when some command execution is unsuccessful, it returns a value, such as: 1, 2, etc.
If-errorlevel
Test code:
Copy Code code as follows:

@ECHO off
XCOPY F:\test.bat D:\
IF ERRORLEVEL 1 ECHO file copy failed
IF ERRORLEVEL 0 ECHO successfully copies files

If the copy of the file is successful, the screen will display "Copy files successfully", or "file copy Failed" will be displayed.
IF errorlevel is used to test the return value of its last DOS command, note that only the return value of the previous command is returned, and the return value must be judged in order from large to small.
The following batch file is therefore incorrect:
@ECHO off
XCOPY C:\AUTOEXEC. BAT D:\
IF ERRORLEVEL 0 ECHO successfully copies files
IF ERRORLEVEL 1 ECHO does not find the copy file
IF ERRORLEVEL 2 ECHO user aborts copy operation via Ctrl-c
IF ERRORLEVEL 3 ECHO preset error prevents file copy operation
Write disk error in the IF errorlevel 4 ECHO copy process
Whether or not the copy succeeds, the following:
Copy file not found
User aborts copy operation via Ctrl-c
Preset errors prevent file copy operations
Write disk error in copy process
will show up.
Copy Code code as follows:

Appendix:
The following are the return values of several commonly used commands and the meaning of their representations:
Backup
0 Backup Successful
1 No backup files found
2 File share conflicts prevent backup from completing
3 User aborts backup with ctrl-c
4 Abort the backup operation due to fatal error
Diskcomp
0 disks are the same
1-Disk comparison different
2 user aborted comparison operation via Ctrl-c
3 The comparison operation aborted due to fatal error
4 Preset Error Abort comparison
diskcopy
0 Disk copy operation succeeded
1 non-fatal disk read/write error
2 user end copy operation via Ctrl-c
3 The disk copy is aborted due to fatal processing error
4 Preset error block copy operation
Format
0 format Success
3 user aborted format processing via CTRL-C
4 formatting aborted due to fatal handling error
5 at the prompt "Proceed with format (y/n)?" Next user type N end
Xcopy
0 successful copying of files
1 No Copy files found
2 user aborted copy operation via Ctrl-c
4 Preset error prevents file copy operation
5 Write disk error in copy process

4, if there is a special use is to detect whether a variable has been defined
Usage examples:
Copy Code code as follows:

@echo off
Set "Str1=ok"
Set "Str2=no"
If defined str1 echo str1 has been defined
If defined str2 echo str2 has been defined
If defined STR3 (Echo STR3 has been defined) else echo STR3 is not defined
Pause>nul

Cond....

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.