Official MS reference for cmd.exe %errorlevel% 9009
Asked Answered
M

1

17

So I know from experience that running a valid program from cmd.exe and checking the %errorlevel% will return a 0:

C:\>dir logo.bmp
 Volume in drive C has no label.
 Volume Serial Number is 5283-A7A2

 Directory of C:\

05/22/2008  12:43 PM         1,440,054 logo.bmp
               1 File(s)      1,440,054 bytes
               0 Dir(s)  71,723,995,136 bytes free

C:\>echo %errorlevel%
0

Likewise, trying to execute a command that does not exist and then checking for the %errorcode% gives me a 9009:

C:\>idontexist.exe
'idontexist.exe' is not recognized as an internal or external command,
operable program or batch file.

C:\>echo %errorlevel%
9009

I've been writing batch scripts for years and they have always worked this way. However, someone has asked questions about compatibility (both forward and backward) of this technique and I can't find any official documentation from Microsoft actually defining 9009 as the errorlevel when a file or program is not found. The closest I've come is this site (http://msdn.microsoft.com/en-us/library/ms681381(v=vs.85).aspx) which, unfortunately, lists 9009 as a DNS error.

Does anyone know where this behavior is documented by Microsoft?

Muss answered 15/4, 2014 at 18:45 Comment(5)
There is a non-MS reference here: en.kioskea.net/faq/2347-error-codes-in-windows. As far as compatibility, batch files aren't going anywhere any time soon if ever.Agle
The DNS_ERROR-thing is SystemErrorCode 9009 Errorlevel (or ExitCode) 9009 is "not recognized as an internal or external command". Although mentioned in some Microsoft-Forums (e.g. social.msdn.microsoft.com/Forums), I'm surprised too, that I seem to be unable to find any "official" list.Humperdinck
@Agle Yes, I found that non-MS reference as well, and even it is missing the 9009 "not recognized as an internal or external command" code. I agree with you, batch files aren't going anywhere, but I'm unfortunately required to provide a reference to the official MS documentation of this specific error code if I want to use it in my script.Muss
I doubt that it's been formally documented, though you might get lucky. It should however be noted that there's nothing stopping any particular application from returning exit code 9009 and meaning something completely different, so it may not be sensible to treat 9009 as a special case.Bacciferous
@HarryJohnston this is a good point, duly noted.Muss
L
20

Microsoft has a Common Error Lookup Tool that will translate error codes (including HRESULTs, which is handy) for you. The output for 9009 is:

# for decimal 9009 / hex 0x2331 :
  MSG_DIR_BAD_COMMAND_OR_FILE                                   cmdmsg.h       
# '%1' is not recognized as an internal or external command,
# operable program or batch file.
  SQL_9009_severity_10                                          sql_err        
# Cannot shrink log file %d (%s) because of minimum log space
# required.
  DNS_ERROR_RCODE_NOTAUTH                                       winerror.h     
# DNS server not authoritative for zone.
# for hex 0x9009 / decimal 36873 :
  SSLEVENT_NO_CIPHERS_SUPPORTED                                 lsapmsgs.mc    
# No suitable default server credential exists on this
# system. This will prevent
# server applications that expect to make use of the system
# default credentials
# from accepting SSL connections. An example of such an
# application is the directory
# server. Applications that manage their own credentials,
# such as the internet
# information server, are not affected by this.
# 4 matches found for "9009"

...so the one you're looking for is from cmdmsg.h.

It's not really documentation, per se, but it's at least official.

Lines answered 15/4, 2014 at 21:44 Comment(3)
It's directly from Microsoft, so it's "official" in that sense and will do what I need. I'd forgotten how difficult it was to find information in closed source systems - working with open source for years has spoiled me. Thanks again!Muss
The web page for error codes. The page in the answer no longer exists.Anticline
Thanks, @DaisukeAramaki! I've updated the answer to correct the link.Lines

© 2022 - 2024 — McMap. All rights reserved.