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 DOS batch processing

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

Share

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

This article mainly shows you the "DOS batch processing if command how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "DOS batch processing if command how to use" this article.

From now on:

Use IF /? in CMD. Open IF's system help (I won't list it all for myself), and we'll find that IF has three basic uses!

Performs conditional processing in a batch program.

IF [NOT] ERRORLEVEL number command

IF [NOT] string1==string2 command

IF [NOT] EXIST filename command

NOT specifies that Windows XP should execute the command only if the condition is false.

ERRORLEVEL number if the last running program returns an exit code equal to or greater than the specified number, the specified condition is true.

String1==string2 if the specified text string matches, the specified condition is true.

EXIST filename if the specified file name exists, the specified condition is true.

Command specifies the command to execute if the condition is met. If the specified condition is FALSE, the command can be followed by an ELSE command that executes the command after the ELSE keyword.

The ELSE clause must appear on the same line after IF. For example:

IF EXIST filename (

Del filename

) ELSE (

Echo filename missing

)

The first usage: IF [NOT] ERRORLEVEL number command

The basic use of this usage is to determine the next step by judging the result of the execution of the previous command.

Generally speaking, the code of the execution result of the last command has only two results. "success" means "failure" and "failure" means 1.

For example:

@ echo offnet userIF% ERRORLEVEL% = = 0 echo net user executed successfully! pause

This is a simple way to judge whether the previous command was executed successfully.

Careful friends may find that this usage is not quite the same as that in help. According to the words "IF% ERRORLEVEL% = = 0 echo net user executed successfully!" the code should be written as: IF ERRORLEVEL 0 echo net user executed successfully!

Then why do I have to write like this? After you change the code and execute it, you will find the error! With this syntax, no matter whether your above command is successful or not, he will think that the command is successful. He doesn't know if it is BUG or if he misunderstands it.

Add: this is not bug, but a feature of the if errorlevel statement: when using if errorlevel 0... If the value of the error code is greater than or equal to 0, an operation will be performed; when using if% errorlevel%==0... If the value of the error code is equal to 0, an operation will be performed. Because of the difference in the meaning of the two sentence patterns, if the former sentence pattern is used, the order of the error code sentences is from big to small.

% ERRORLEVEL% this is a system variable that returns the code for the execution result of the previous command! Success is represented by 0 and failure by 1. Of course, there are other parameters, when using these two basic numbers.

Generally speaking, the code of the execution result of the last command has only two results. "success" means "failure" and "failure" means 1.

This is only a general case. In fact, the return value of errorlevel can be between 0,255 and 255. for example, there are five xcopy default errorlevel values, each representing five execution states:

Exit code description

There is no error in copying the 0 file.

1 if errorlevel 2 echo .

2 the user terminated the xcopy by pressing CTRL+C.

4 an initialization error occurred. There is not enough memory or disk space, or an invalid drive name or syntax was entered on the command line.

5 A disk write error occurred.

To determine the five exits of the above xcopy command, you should write:

Disk write error occurred in if errorlevel 5 echo

Initialization error occurred in if errorlevel 4 echo

If errorlevel 2 echo user terminated xcopy by CTRL+C

If errorlevel 1 echo if errorlevel 2 echo

There is no error in copying the if errorlevel 0 echo file.

In order to execute it correctly.

Replenish it.

Give a few more examples for beginners to understand.

@ echo offnet usertestIF% ERRORLEVEL% = = 1 echo net user execution failed! pause

This is to judge whether the execution of the previous command failed.

@ echo offset / p var= casually enter a command:% var%if% ERRORLEVEL% = = 0 goto yesgoto no:yesecho! var! The execution succeeded. Pauseexit:noecho basically failed.. pause

This is based on the command you enter, automatically judge whether it is a success or a failure!

Let's have a simplified version.

@ echo offset / p var= casually enter a command:% var%if% ERRORLEVEL% = = 0 (echo% var% execution succeeded) ELSE echo% var% execution failed! pause

Write the operation after the failed execution after the else!

Of course, we can also divide a sentence like if else into several lines to make it look better.

@ echo offset / p var= casually enter a command:% var%if% ERRORLEVEL% = = 0 (echo! var! The execution was successful) ELSE (echo basically failed. ) pause

The two abbreviations introduced here can be applied to the three grammars of IF, not only in the method of IF [NOT] ERRORLEVEL number command.

Second usage: IF [NOT] string1==string2 command

This is used to compare whether the values of variables or characters are equal.

Examples

@ echo offset / p var= Please enter the first comparison character: set / p var2= Please enter the second comparison character: if% var% = =% var2% (echo we are equal) ELSE echo we are not equal pause

The above example can determine whether the values you enter are equal, but if you enter the same characters, but if one of them is followed by a space

This example will still be considered equal, how to make the input with spaces not equal? We can just put a double quotation mark on the comparison character.

@ echo offset / p var= Please enter the first comparison character: set / p var2= Please enter the second comparison character (try entering more spaces): if "% var%" = = "% var2%" (echo we are equal) ELSE echo We are not equal pause

The third usage: IF [NOT] EXIST filename command

This is the syntax to determine whether a file or folder exists.

Examples

@ echo offif exist "c:\ test" (echo exists file) ELSE echo does not exist file pause

The purpose of judging the file path in quotation marks is to prevent the path from having spaces. If the path has spaces and double quotation marks, there will be no judgment errors!

There are not many uses of this grammar, so that's it, so there won't be much introduction.

In addition, we see that every IF usage is followed by a [NOT] statement. What does that mean? In addition to his words, it means that when we first judge that our conditions are not valid, if we do not add him by default, when the judgment conditions are established, such as the above example

@ echo offif not exist "c:\ test" (echo exists file) ELSE echo does not exist file pause

Add a NOT, and what is the result after execution? if there is no c:\ test under your C disk, it will still display "existence file", which means that if you add NOT, you will fail to judge the condition first! See, the above example is changed to this is correct!

@ echo offif not exist "c:\ test" (echo does not exist file) ELSE echo exists file pause

The fourth usage: the use of IF enhancement

IF [/ I] string1 compare-op string2 command # parameter / I indicates case insensitivity

IF CMDEXTVERSION number command

IF DEFINED variable command # is useful to determine whether a variable exists or not

The CMDEXTVERSION condition works the same as ERRORLEVEL, except that it is compared to the build number associated with the command extension. The first version is 1. Each time there is a considerable enhancement to the command extension, the version number is increased by one. The CMDEXTVERSION condition is not true when the command extension is disabled.

If an environment variable is defined, the DEFINED condition works the same as EXISTS

IF DEFINED variable command

IF NOT "variable" = "" command

The above two commands have the same effect.

Use the "set variable=" command to make the variable variable undefined, or null.

In a word, if the variable value is empty, it is undefined; if the variable value is not empty, it is defined.

When using the statement IF DEFINED variable command to determine whether a variable exists, please note that variable is a variable name that does not use the boot symbol%, and cannot be written as% variable%, or there will be an error.

Example:

If defined aa (echo variable aa exists) else (echo variable aa does not exist)

Run shows that the variable aa does not exist

Example:

Set aa=123

Set aa=

If defined aa (echo variable aa exists) else (echo variable aa does not exist)

Run shows that the variable aa does not exist

Example:

@ echo offif a = = A (echo we are equal) ELSE echo we are not equal pause

After execution, it will be displayed: we are not equal.

Example:

@ echo offif / Ia = = A (echo we are equal) ELSE echo we are not equal pause

Plus / I is case-insensitive and it is equal!

At the end, there are some symbols used to judge numbers.

EQU-equals

NEQ-not equal to

LSS-less than

LEQ-less than or equal to

GTR-greater than

GEQ-greater than or equal to

Let me give you an example. Everyone knows math. Don't talk too much.

@ echo offset / p var= Please 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 4pause is all the content of this article "how to use if commands in DOS 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