Sep 09 2008

Windows Shell Exit/Return Code Variable

Published by Matt at 3:41 pm under Technology

After running a command in Unix, the status of the command (usually a simply success or failure, 0/1) can be found by echoing the $? environment variable.

 

Of course, you can do this in Windows too, but I guess no one really does because it is hard to find searching the Internet.  So, to determine an application’s exit code from a Windows shell, first run it, and then echo the value of the environment variable %ERRORLEVEL%.  This should give you the same information as the relevant command in Unix.

 
C:\Documents and Settings\mfaus>dir doesNotExist
Volume in drive C has no label.
Volume Serial Number is F08F-730C

Directory of C:\Documents and Settings\mfaus
File Not Found

C:\Documents and Settings\mfaus>echo %ERRORLEVEL%
1

C:\Documents and Settings\mfaus>dir NewDirectory
Volume in drive C has no label.
Volume Serial Number is F08F-730C

Directory of C:\Documents and Settings\mfaus\NewDirectory

09/09/2008 04:39 PM

.
09/09/2008 04:39 PM
..
0 File(s) 0 bytes
2 Dir(s) 172,309,262,336 bytes free

C:\Documents and Settings\mfaus>echo %ERRORLEVEL%
0

Hope this helps someone out there!

5 responses so far

5 Responses to “Windows Shell Exit/Return Code Variable”

  1. Brendaon 10 Sep 2008 at 1:31 pm

    Well I certainly feel enlightened. I was looking for a Windows exit code just the other day. Of course this is greek to me but I appreciate the detail and work that you must have put in to accomplish this solution. Way to go. I love you, Mom

  2. Matton 10 Sep 2008 at 1:53 pm

    Haha, thanks mom. It really didn’t take much time to figure out, I just had to dig around on the Internet longer than 5 minutes. Hopefully, the next time someone else searches for these keywords this blog post will show up #1 in Google and answer their question immediately.

  3. Darrenon 09 Oct 2008 at 6:38 am

    This was useful for me, although it wasn’t #1 Google search result. :)

    ;D

  4. Mattimuson 12 Jan 2009 at 8:51 am

    This works. Gracias!

  5. Pavel Vlasovon 06 Apr 2011 at 8:00 am

    THank you! (It was #1 google search result.)

Trackback URI | Comments RSS

Leave a Reply