乐闻世界logo
搜索文章和话题

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

1个答案

1

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.

  1. Open the Command Prompt:

    • To open the Command Prompt, search for 'cmd' or 'Command Prompt' in the search bar and launch it.
  2. Run the Application:

    • To execute the application, enter its path and name in the command line. For example, to run example.exe located in the D:\Programs folder, input:
      shell
      D:\Programs\example.exe
  3. 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 0 typically indicates successful execution, while non-zero values usually signify an error occurred.

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:

cmd
C:\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.

2024年7月24日 09:51 回复

你的答案