Bat batch if command example explanation

Time:2021-4-27

if   Command examples

If, as it means in E, means “if”, which is used for conditional judgment. If a certain condition is met, the following command will be executed.
Mainly used to determine, 1, two “string” is equal; 2. The two values are greater than, less than and equal to, and then execute the corresponding command.
Of course, there are special uses, such as combining errorlevel: if   errorlevel   one   echo   error
Or with defined: if   defined   test  ( echo   It   is   defined)   else   echo   It   isn’t   defined

Usage example:
1. Check the string (note that if is used to check whether the strings are equal, it is not “=”, but “=”):

Copy codeThe code is as follows:
@echo off
set “str=this is a test”
REM   Check whether the variable% STR% is equal to test. If it is equal, OK will be displayed, otherwise no will be displayed
if “%str%”==”test” (echo OK) else echo NO
pause>nul

2. Detection value: (note that the greater than symbol in batch processing cannot be used “>”, but “GTR”, others are similar)
List of comparison operators:
EQU  –  be equal to
NEQ  –  Not equal to
LSS  –  less than
LEQ  –  Less than or equal to
GTR  –  greater than
GEQ  –  Greater than or equal to
Detection value   twenty   Is it greater than   15 demo code:

Copy codeThe code is as follows:
@echo off
set /a num1=20
set /a num2=15
if  % num1%   gtr  % num2%   echo  % Num1% 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:
Note: the initial value of the environment variable errorlevel is 0. When some commands are not executed successfully, a value will be returned, such as 1  , two   etc.
IF-ERRORLEVEL 
Test code:

Copy codeThe code is as follows:
@ECHO OFF 
XCOPY F:\test.bat D:\ 
IF   ERRORLEVEL   one   ECHO   File copy failed  
IF   ERRORLEVEL   0   ECHO   File copied successfully  

If the file is copied successfully, the screen will display “copy file successfully”, otherwise it will display “file copy failed”.  
IF   ERRORLEVEL   It 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 the order from large to small.  
So the following batch file is wrong:  
@ECHO OFF 
XCOPY C:\AUTOEXEC.BAT D:\ 
IF   ERRORLEVEL   0   ECHO   File copied successfully  
IF   ERRORLEVEL   one   ECHO   Copy file not found  
IF   ERRORLEVEL   two   ECHO   The user aborts the copy operation through ctrl-c  
IF   ERRORLEVEL   three   ECHO   Preset error prevents file copy operation  
IF   ERRORLEVEL   four   ECHO   Disk writing error during copy  
Whether the copy is successful or not, the following:  
Copy file not found  
The user aborts the copy operation through ctrl-c  
Preset error prevents file copy operation  
Disk writing error during copy  
All will be displayed.

Copy codeThe code is as follows:
Appendix:
The following are the return values of several common commands and the meanings they represent:  
backup 
0   Backup succeeded  
one   Backup file not found  
two   File sharing conflicts prevent backup from completing  
three   User aborts backup with ctrl-c  
four   The backup operation was aborted due to a fatal error  
diskcomp 
0   It’s the same  
one   It’s different  
two   The user aborts the comparison operation through ctrl-c  
three   The comparison operation was aborted due to a fatal error  
four   Preset error abort comparison  
diskcopy 
0   Disk copy operation successful  
one   Non fatal disk read / write errors  
two   The user ends the copy operation with ctrl-c  
three   Disk copy aborted due to fatal processing error  
four   Preset error prevents copy operation  
format 
0   Format successfully  
three   The user aborts the format processing through ctrl-c  
four   Format aborted due to fatal processing error  
five   At the prompt “proceed”   with   format(y/n)?” The next user type n to end  
xcopy 
0   File copied successfully  
one   Copy file not found  
two   The user aborts the copy operation through ctrl-c  
four   Preset error prevents file copy operation  
five   Disk writing error during copy  

4. Another special use of if is to detect whether a variable has been defined
Usage example:

Copy codeThe code is 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   It’s not defined
pause>nul

To be continued

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]