Skip to content

Latest commit

 

History

History
executable file
·
149 lines (102 loc) · 4.47 KB

File metadata and controls

executable file
·
149 lines (102 loc) · 4.47 KB

⚠️ Understanding ERRORLEVEL and IF ERRORLEVEL

  • In Windows Batch scripting, ERRORLEVEL is used to check the exit code of the last executed command or program.

    It's a critical tool for error handling and control flow, especially in automation, scripting pipelines, or conditionally executing code.

    🧩 What is ERRORLEVEL?

    • ERRORLEVEL is an environment variable that stores the exit code of the last run program or script.

    • Most commands and utilities in Windows return an exit code:

      • 0 usually means success
      • Any other number (e.g., 1, 2, 9009, etc.) indicates some kind of failure

      🔍 Example

      @ECHO OFF
      DIR "C:\Some\Fake\Path"
      ECHO Last exit code was: %ERRORLEVEL%
      • If the path doesn't exist, %ERRORLEVEL% will be non-zero.

      ✅ Common Exit Code Meanings

      Code Meaning
      0 Success
      1 General error
      2 Incorrect usage / syntax
      9009 Command not found

      🔎 IF ERRORLEVEL — How It Works

      The IF ERRORLEVEL syntax is a bit non-intuitive for beginners:

      IF ERRORLEVEL n command

      This checks if %ERRORLEVEL% is greater than or equal to n, not just equal to n.

      ⚠️ Important:

      IF ERRORLEVEL 1

      means:

      IF %ERRORLEVEL% GEQ 1
      

      So, if %ERRORLEVEL% is 5, the condition will still match!


  1. 🧪 Example 1: Check if command failed

    @ECHO OFF
    COPY file.txt D:\backup\
    IF ERRORLEVEL 1 (
        ECHO Copy failed!
    ) ELSE (
        ECHO Copy succeeded.
    )
  2. 🧪 Example 2: Exact Match (Use Order Carefully!)

    If you want to check for a specific code, use descending order:

    @ECHO OFF
    SomeCommand
    
    IF ERRORLEVEL 3 (
        ECHO Exit code was 3 or more
    ) ELSE IF ERRORLEVEL 2 (
        ECHO Exit code was 2 or more
    ) ELSE IF ERRORLEVEL 1 (
        ECHO Exit code was 1 or more
    ) ELSE (
        ECHO Exit code was 0
    )

    ☑️ Always list the highest values first due to the GEQ behavior.

  3. 🧪 Example 3: Setting Your Own ERRORLEVEL

    Use EXIT /B [number] to set your own error code inside a script or subroutine:

    @ECHO OFF
    CALL :Check 0
    CALL :Check 1
    GOTO :EOF
    
    :Check
    ECHO Checking with exit code %1
    EXIT /B %1

    Then use %ERRORLEVEL% or IF ERRORLEVEL to control behavior based on the result.


📌 Best Practices

Tip Why
Use IF ERRORLEVEL in descending order Prevents multiple matches
Use EXIT /B n in subroutines To simulate return codes
Reset %ERRORLEVEL% if needed Some commands (like SET) don’t reset it
Combine with ` and&&` for simpler flow Reduces nested blocks

➕ Shortcut: && and ||

SomeCommand && ECHO Success || ECHO Failed
  • && runs the next command only if the previous succeeded (ERRORLEVEL = 0)
  • || runs the next command only if the previous failed (ERRORLEVEL > 0)

This behaves like a shorthand IF ERRORLEVEL check.


✅ Summary Table

Concept Usage Example Notes
Check exit code ≥ n IF ERRORLEVEL 2 GEQ behavior
Exact code check Descending IF ERRORLEVEL blocks Recommended
View current error ECHO %ERRORLEVEL% Shows last result
Set custom code EXIT /B 3 Manual control
Short condition `cmd && echo OK echo Fail`