When running an application from the Windows Command Prompt (CMD), you can retrieve the exit code of the application in several ways. The exit code is a numeric value that indicates whether the program executed successfully or encountered an error during execution.
-
Open the Command Prompt:
- To open the Command Prompt, search for 'cmd' or 'Command Prompt' in the search bar and launch it.
-
Run the Application:
- To execute the application, enter its path and name in the command line. For example, to run
example.exelocated in theD:\Programsfolder, input:shellD:\Programs\example.exe
- To execute the application, enter its path and name in the command line. For example, to run
-
Use
echo %ERRORLEVEL%to Retrieve the Exit Code:- After the program finishes executing, immediately enter the command:
shell
echo %ERRORLEVEL% - This command displays the exit code of the previously executed program. An exit code of
0typically indicates successful execution, while non-zero values usually signify an error occurred.
- After the program finishes executing, immediately enter the command:
Example
Suppose an application named example.exe returns an exit code of 0 when successful and 1 when it fails. The following operations are performed in the Command Prompt:
cmdC:\Users\YourUsername> D:\Programs\example.exe C:\Users\YourUsername> echo %ERRORLEVEL% 0
In this example, the echo %ERRORLEVEL% command displays 0, confirming that example.exe executed successfully. If the program encounters an error, %ERRORLEVEL% will show the error code, such as 1 or other custom codes, depending on how the application is designed to report issues.
Using this method, you can easily retrieve and verify the exit code of any application from the Windows command line. This approach is particularly valuable in automation scripts or batch files, as it allows you to conditionally execute different commands or operations based on the exit code.