Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use if command in bat batch processing

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use the if command in bat batch processing, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Detailed explanation of if command example

If, as it means in E, means "if" and is used to make conditional judgments. If a certain condition is met, the following command will be carried out.

Mainly used to determine, 1, two "strings" are equal; 2, two values are greater than, less than, equal to, and then execute the corresponding command.

Of course, there are special uses, such as combining errorlevel:if errorlevel 1 echo error.

Or combine defined (defined meaning): if defined test (echo It is defined) else echo It isn't defined

Usage example:

1. Detect the string (note that when you use if to detect whether the string is equal, it is followed by "=" instead of "="):

The code is as follows:

@ echo off

Set "str=this is a test"

REM detects whether the variable% str% is equal to test, and if so, displays OK, otherwise displays NO

If "% str%" = = "test" (echo OK) else echo NO

Pause > nul

2. Check the value: (note that the larger than symbol cannot be used in batch processing: ">", but with "gtr", others are similar)

List of comparison operators:

EQU-equals

NEQ-not equal to

LSS-less than

LEQ-less than or equal to

GTR-greater than

GEQ-greater than or equal to

Check whether the value 20 is greater than 15 demo code:

The code is 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. Use if in combination 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, 2, etc.

IF-ERRORLEVEL

Test the code:

The code is as follows:

@ ECHO OFF

XCOPY F:\ test.bat D:\

IF ERRORLEVEL 1 ECHO file copy failed

IF ERRORLEVEL 0 ECHO successfully copied the file

If the file copy is successful, the screen displays "copy file successfully", otherwise "file copy failed" is displayed.

IF ERRORLEVEL is used to test the return value of its last DOS command, note that it is only the return value of the previous command, and the return value must be judged in order from highest to lowest.

So the following batch file is incorrect:

@ ECHO OFF

XCOPY C:\ AUTOEXEC.BAT D:\

IF ERRORLEVEL 0 ECHO successfully copied the file

IF ERRORLEVEL 1 ECHO could not find the copy file

IF ERRORLEVEL 2 ECHO user aborts copy operation through ctrl-c

IF ERRORLEVEL 3 ECHO preset error prevents file copy operation

IF ERRORLEVEL 4 disk write error during ECHO copy

Regardless of 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

Write disk error during copy

Will be displayed.

The code is as follows:

Appendix:

Here are the return values of several commonly used commands and what they represent:

Backup

0 backup successful

1 backup file not found

2 File sharing conflicts prevent backups from being completed

3. Users abort backup with ctrl-c

4 the backup operation was aborted due to a fatal error

Diskcomp

0 disk is the same.

One set is different.

2 the user aborts the comparison operation through ctrl-c

3 the comparison operation is aborted due to a fatal error

4 comparison of preset error abort

Diskcopy

0 disk copy operation succeeded

1 non-fatal disk read / write error

2 the user ends the copy operation through ctrl-c

3 abort the copy of the disk due to a fatal processing error

4 preset error prevents copy operation

Format

0 formatted successfully

3 the user aborts the formatting process through ctrl-c

4 the format was aborted due to a fatal processing error

5 prompting "proceed with format (yzone)?" The next user types n to end.

Xcopy

0 successfully copied the file

1 copy file not found

2 the user aborts the copy operation through ctrl-c

4 preset error prevents file copy operation

5 disk writing error during copy

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

Usage example:

The 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 is not defined

Pause > nul

The above is all the contents of this article entitled "how to use if commands in bat batches". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report