How do I get the application exit code from a Windows command line?

I am running a program and want to see what its return code is (since it returns different codes based on different errors).

I know in Bash I can do this by running

echo $?

What do I do when using cmd.exe on Windows?

Answers


There are two ways:

  1. The results are stored in a pseudo environment variable named errorlevel so...

    echo Exit Code is %errorlevel%
    
  2. and a special syntax of the if command:

    if errorlevel
    

See if /? for details.

For Example

@echo off
my_nify_exe.exe
if errorlevel 1 (
   echo Failure Reason Given is %errorlevel%
   exit /b %errorlevel%
)

Warning: If you set an environment variable name errorlevel, %errorlevel% will return that value and not the exit code. Use (set errorlevel=) to clear the environment variable, allowing access to the true value of errorlevel via the %errorlevel% environment variable.


Need Your Help

Looking for ruby gem that keeps ruby environment loaded

ruby-on-rails console rake

I'm not a fan of waiting for rake -T to get back to me. About a year ago, I found a ruby gem -- I think it was called the "Rails Console" or the "Ruby Console" or something like that -- that kept the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.