Dos On Error Level
Contents |
Chen - MSFTSeptember 26, 200815 0 0 0 The command interpreter cmd.exe has a concept known as the error level, which is the exit code of the program most recently run. You can test the error level with the IF ERRORLEVEL command:
Dos Errorlevel
IF ERRORLEVEL 1 ECHO error level is 1 or more
Error Level Analysis
expressed different degrees of failure with higher and higher exit codes. For example, the diff program has three exit codes: 0 means the files are the same; 1 means the files are different; dos batch error level 2 means that something terrible happened. There are also programs that use an exit code of zero to mean success and anything else to mean failure. In addition to this internal state, you can, if you wish, create an environment variable with the name ERRORLEVEL, in the same way that you can create an environment variable called FRED. But, as with FRED, that variable won't have dos error 5 any effect on the error level. rem this next command sets the error level to zero CMD /C EXIT 0 set ERRORLEVEL=1 if ERRORLEVEL 1 echo Does this print? The message is not printed because the ERRORLEVEL environment variable has no effect on the error level. It's just a variable whose name happens to coincide with a command processor concept. set BANKBALANCE=$1,000,000.00 "Hey, when I tried to withdraw the money, I got an insufficient funds error. What am I doing wrong?" Now, it does happen to be the case that if command extensions are enabled and you say %ERRORLEVEL%, then the command processor first looks for an environment variable called ERRORLEVEL, and if it can't find one, then it replaces %ERRORLEVEL% with the current value of the internal error level value. It's a fallback step, in the same way that your neighbor is a fallback delivery location if you aren't home. If you file a change-of-address form for yourself, that doesn't affect packages sent to your neighbor. The same behavior can be seen with %CD%: If you did not explicitly set an environment variable called CD, then %CD% expands to the command processor's current directory. But you
stdin, stdout, stderr Part 5 – If/Then Conditionals Part 6 – Loops Part 7 – Functions Part 8 – Parsing Input Part 9 – Logging Part 10 – Advanced Tricks Today we’ll
Dos Error 64
cover return codes as the right way to communicate the outcome of your script’s dos error 4 execution to the world. Sadly, even skilled Windows programmers overlook the importance of return codes. Return Code Conventions By convention, command line
Dos Error Code 1
execution should return zero when execution succeeds and non-zero when execution fails. Warning messages typically don’t effect the return code. What matters is did the script work or not? Checking Return Codes In Your Script https://blogs.msdn.microsoft.com/oldnewthing/20080926-00/?p=20743 Commands The environmental variable %ERRORLEVEL% contains the return code of the last executed program or script. A very helpful feature is the built-in DOS commands like ECHO, IF, and SET will preserve the existing value of %ERRORLEVEL%. The conventional technique to check for a non-zero return code using the NEQ (Not-Equal-To) operator of the IF command: IF %ERRORLEVEL% NEQ 0 ( REM do something here to address the error ) Another common http://steve-jansen.github.io/guides/windows-batch-scripting/part-3-return-codes.html technique is: IF ERRORLEVEL 1 ( REM do something here to address the error ) The ERRORLEVEL 1 statement is true when the return code is any number equal to or greater than 1. However, I don’t use this technique because programs can return negative numbers as well as positive numbers. Most programs rarely document every possible return code, so I’d rather explicity check for non-zero with the NEQ 0 style than assuming return codes will be 1 or greater on error. You may also want to check for specific error codes. For example, you can test that an executable program or script is in your PATH by simply calling the program and checking for return code 9009. SomeFile.exe IF %ERRORLEVEL% EQU 9009 ( ECHO error - SomeFile.exe not found in your PATH ) It’s hard to know this stuff upfront – I generally just use trial and error to figure out the best way to check the return code of the program or script I’m calling. Remember, this is duct tape programming. It isn’t always pretty, but, it gets the job done. Conditional Execution Using the Return Code There’s a super cool shorthand you can use to execute a second command based on the success or failure of a command. The first program/scri
File not found 03 Path not found 04 Too many open files (no handles left) 05 Access denied 06 Invalid handle 07 Memory control blocks destroyed 08 Insufficient memory 09 Invalid memory http://stanislavs.org/helppc/dos_error_codes.html block address 0A Invalid environment 0B Invalid format 0C Invalid access mode (open mode is invalid) 0D Invalid data 0E Reserved 0F Invalid drive specified 10 Attempt to remove current directory 11 Not same device 12 No more files 13 Attempt to write on a write-protected diskette 14 Unknown unit 15 Drive not ready 16 Unknown command 17 CRC error 18 Bad request structure length 19 Seek error 1A Unknown media type 1B Sector dos error not found 1C Printer out of paper 1D Write fault 1E Read fault 1F General failure 20 Sharing violation 21 Lock violation 22 Invalid disk change 23 FCB unavailable 24 Sharing buffer overflow 25 Reserved 26 Unable to complete file operation (DOS 4.x) 27-31 Reserved 32 Network request not supported 33 Remote computer not listening 34 Duplicate name on network 35 Network name not found 36 Network busy 37 Network device no longer exists 38 dos error code NetBIOS command limit exceeded 39 Network adapter error 3A Incorrect network response 3B Unexpected network error 3C Incompatible remote adapter 3D Print queue full 3E No space for print file 3F Print file deleted 40 Network name deleted 41 Access denied 42 Network device type incorrect 43 Network name not found 44 Network name limit exceeded 45 NetBIOS session limit exceeded 46 Temporarily paused 47 Network request not accepted 48 Print or disk redirection is paused 49-4F Reserved 50 File already exists 51 Reserved 52 Cannot make directory entry 53 Fail on INT 24 54 Too many redirections 55 Duplicate redirection 56 Invalid password 57 Invalid parameter 58 Network device fault 59 Function not supported by network (DOS 4.x) 5A Required system component not installed (DOS 4.x) DOS Error Code/Classes Error Classes 01 Out of resource, out of space, channel, etc 02 Temporary situation, not an error, ex: file lock 03 Authorization, permission denied 04 Internal, system detected internal error 05 Hardware failure, serious problem related to hardware 06 System failure, ex: invalid configuration 07 Application error, inconsistent request 08 Not found, file/item not found 09 Bad format, file/item in invalid format 0A Locked, file/item interlocked 0B Media failure, ECC/CRC error, wrong or bad disk 0C Already exists, collision with existing item 0D Unknown, classification doesn't exist or is inappropriate D