If-errorlevel Use Method _dos/bat

Source: Internet
Author: User
Tags file copy goto mssqlserver

Today, a remote customer, found that his server will be automatically disconnected from the network, causing the client can not connect the service side, but restart the service and return to normal after the initial judgment may be due to a system service and WINPACP conflict, recommend users to restore the operating system, he wanted to wait until the evening to start operation Today you have to run a ping, found timed out on the manual restart service. I remember that if you could use the return value of a program as a judge, you could write this script for him:

Copy Code code as follows:

@echo off
: P
Ping 61.152.XX. Xx
IF errorlevel 1 goto AA
IF errorlevel 0 goto BB
: AA
Echo is restarting the software service ...
net stop MSSQLServer
NET start MSSQLServer
Goto P
: BB
Echo continues the ping ...
Goto P

The function of this command is to ping 61.152.XX. XX This IP, if the ping does not general restart MSSQL Services, and then continue ping, if you can ping, or continue ping ...

We all know if is a conditional judgment statement under the command line, ERRORLEVEL is one of its parameters, translated is the "error return code" meaning, its role is to determine the previous command error return value, and then the definition of the character value comparison, and then decide what action to take.
The format is this:

if errorlevel Nubmer commend
Example: if ERRORLEVEL 1 echo return value equals 1

So people are going to ask, how do I get the return value of the previous command?
The method is very simple, take ping to say, we first run a ping 127.0.0.1
The run result is reply from 127.0.0.1:bytes=32 time<1ms ttl=128
When we enter the Echo%errorlevel%, we can see that the return value is 0,
And then we ping a non-existent IP, like 192.192.192.192.
The ping command shows the Request timed out.
OK, we're using echo%errorlevel% to find out what the return value is.
Now it's 1.

You can also use this method to query the return values of other commands, such as copy, Dir, and so on.
Of course if errorlevel a comparison of numeric values is not limited to equal. In Microsoft's help we can see:

IF%errorlevel% Leq 1 goto okay

The leq here means "less than equals" and all the comparison parameters are as follows:
EQU-equals
NEQ-Not equal to
LSS-Less than
Leq-less than or equal to
GTR-Greater than
GEQ-greater than or equal to

If there is a mistake, thank you.



If-errorlevel

The establishment of the Test4.bat, the contents are as follows:
@ECHO off
XCOPY C:\AUTOEXEC. BAT D:\
IF ERRORLEVEL 1 ECHO file copy failed
IF ERRORLEVEL 0 ECHO successfully copies files
Then execute the file:
C:\>test4
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.
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

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.