-->

  • ERRORLEVEL in Batch Scripting with Example

     

    ERRORLEVEL in Batch Scripting with Example

     

     

     ERRORLEVEL in Batch Scripting with Example


    When running commands in Batch Scripting, it is important to know whether a command was executed successfully or failed.



    This is where the **ERRORLEVEL** variable becomes useful.

    `ERRORLEVEL` helps us check the result of the previously executed command and take action based on success or failure.



    In this tutorial, we will learn how to use ERRORLEVEL with a practical example.

     What is ERRORLEVEL?


    `ERRORLEVEL` is a special system variable in Batch Scripting.


    After a command executes:

    * `0` usually means **Success**
    * Any non-zero value usually means **Error or Failure**



    Example


    text
    ERRORLEVEL = 0


    Means the command completed successfully.

    • ERRORLEVEL = 1



    Means an error occurred.



    Example: Checking a Folder Path


      @echo off
    
    set /P FolderPath=Enter the folder path:
    
     cd %FolderPath%
    
    if %ERRORLEVEL% EQU 0 (
          echo You entered the Correct path: %FolderPath% and it will exists
    ) else (
            echo You entered the Wrong path: %FolderPath%
    )
    
    
      

    How the Script Works


    Step 1

    The script asks the user to enter a folder path.

    set /P FolderPath=Enter the folder path:


    Example:

    C:\Users\user\Desktop


    Step 2

    The script attempts to change to the specified directory.


    cd %FolderPath%


    If the folder exists:

    ERRORLEVEL = 0



    If the folder does not exist:

    ERRORLEVEL = 1


    (or another non-zero value)


    Step 3

    The IF statement checks ERRORLEVEL.

    if %ERRORLEVEL% EQU 0

    If successful:

    • You entered the Correct path

    Otherwise:

    • You entered the Wrong path



    Example Output (Valid Path)

    • C:\Users\user\Desktop



    Output

    You entered the Correct path: C:\Users\user\Desktop and it will exists




    Example Output (Invalid Path)


    Input
    C:\InvalidFolder

    Output
    You entered the Wrong path: C:\InvalidFolder




    Why is ERRORLEVEL Important?



    ERRORLEVEL helps us:

    • * Detect command failures
    • * Validate user input
    • * Handle errors automatically
    • * Improve script reliability
    • * Create professional automation scripts


    Without ERRORLEVEL, a script may continue running even when something goes wrong.



    Real-World Uses for System Engineers


    System Engineers often use ERRORLEVEL for:

    • * Checking backup success
    • * Verifying software installation
    • * Network troubleshooting
    • * Validating file paths
    • * Monitoring scheduled tasks
    • * Automating maintenance scripts


    It is one of the most commonly used error-checking techniques in Batch Scripting.



    Interview Questions



    What is ERRORLEVEL in Batch Scripting?
    ERRORLEVEL is a system variable that stores the result of the last executed command.


    What does ERRORLEVEL 0 mean?
    It usually indicates that the command executed successfully.


    What does a non-zero ERRORLEVEL mean?
    It usually indicates an error or failure.


    Why is ERRORLEVEL used?
    It helps scripts detect and handle errors automatically.


    Can ERRORLEVEL be used with IF statements?
    Yes. It is commonly used with IF statements to check command results.



    Conclusion



    ERRORLEVEL is an essential feature in Batch Scripting that allows you to determine whether a command succeeded or failed. By combining ERRORLEVEL with IF ELSE statements, you can create smarter and more reliable scripts that handle errors effectively.

    As a future System Engineer or SecDevOps professional, understanding ERRORLEVEL will help you build automation scripts that are easier to troubleshoot and maintain.

  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.