Batch Script - Return Code

Hello there, future programmers! Today, we're going to dive into an exciting topic in Batch scripting: Return Codes. Don't worry if you're completely new to programming; I'll guide you through this journey step by step, just like I've done with countless students over my years of teaching. So, grab a cup of coffee (or your favorite beverage), and let's get started!

Batch Script - Return Code

Understanding Return Codes

Before we jump into the nitty-gritty, let's understand what return codes are. Imagine you're sending your robot friend on a mission. When it comes back, you'd want to know if the mission was successful, right? That's exactly what return codes do in programming. They're like little messages that programs send back to tell us whether they completed their task successfully or if something went wrong.

In Batch scripting, these return codes are often referred to as "error levels". Let's explore this concept further.

Error Level

In Batch, the error level is a special variable that holds the return code of the last executed command or program. It's like a scorecard that tells us how well (or poorly) a command performed.

Checking the Error Level

To check the error level, we use the %ERRORLEVEL% variable. Here's a simple example:

@echo off
dir C:\NonExistentFolder
echo The error level is %ERRORLEVEL%

If you run this script, you'll see something like this:

File Not Found
The error level is 1

The dir command couldn't find the folder, so it returned an error level of 1. If the folder existed, the error level would be 0.

Using Error Levels in Decision Making

Error levels are super useful for making decisions in your scripts. Here's an example:

@echo off
ping google.com
if %ERRORLEVEL% EQU 0 (
    echo Internet connection is working!
) else (
    echo Oops! No internet connection.
)

In this script, we're checking if we can ping Google. If the ping is successful (error level 0), we know the internet is working. If not, we display an error message.

Loops

Now that we understand error levels, let's look at how we can use them in loops. Loops are like a merry-go-round for your code, allowing you to repeat actions until a certain condition is met.

Basic Loop Structure

Here's a simple loop that counts from 1 to 5:

@echo off
set counter=1
:loop
if %counter% LEQ 5 (
    echo Counter is %counter%
    set /a counter+=1
    goto loop
)
echo Loop finished!

This script will output:

Counter is 1
Counter is 2
Counter is 3
Counter is 4
Counter is 5
Loop finished!

Using Error Levels in Loops

We can combine loops with error levels to create more dynamic scripts. Here's an example:

@echo off
:retry
ping google.com
if %ERRORLEVEL% NEQ 0 (
    echo Connection failed. Retrying in 5 seconds...
    timeout /t 5 >nul
    goto retry
)
echo Connection successful!

This script will keep trying to ping Google until it succeeds. It's like a persistent little robot that won't give up until it completes its mission!

Looping through Command Line Arguments

Command line arguments are like special instructions you give to your script when you run it. Let's see how we can loop through these arguments.

Basic Argument Loop

Here's a script that prints out all the arguments you give it:

@echo off
:loop
if "%1"=="" goto end
echo Argument: %1
shift
goto loop
:end
echo All arguments processed!

If you run this script with script.bat apple banana cherry, it will output:

Argument: apple
Argument: banana
Argument: cherry
All arguments processed!

Using Error Levels with Arguments

Let's create a more advanced script that checks if the files passed as arguments exist:

@echo off
:loop
if "%1"=="" goto end
if exist %1 (
    echo %1 exists
) else (
    echo %1 does not exist
    set ERRORLEVEL=1
)
shift
goto loop
:end
if %ERRORLEVEL% NEQ 0 (
    echo Some files were not found
) else (
    echo All files exist
)

This script checks each file passed as an argument. If any file doesn't exist, it sets the error level to 1. At the end, it tells you if all files were found or if some were missing.

Conclusion

Congratulations! You've just taken your first steps into the world of Batch scripting with return codes. Remember, like learning any new language, practice is key. Don't be afraid to experiment and make mistakes – that's how we all learn!

Here's a quick reference table of the methods we've covered:

Method Description
%ERRORLEVEL% Variable to check the last command's return code
if %ERRORLEVEL% EQU 0 Check if the last command was successful
goto label Jump to a specific label in the script
set /a variable+=1 Increment a variable
shift Move to the next command line argument
if exist filename Check if a file exists

Keep coding, keep learning, and most importantly, have fun! Remember, every expert was once a beginner. Who knows? Maybe one day you'll be writing your own programming tutorials!

Credits: Image by storyset