Batch Script - Variables

Hello, future programmers! Today, we're diving into the exciting world of Batch Script variables. As your friendly neighborhood computer teacher, I'm here to guide you through this journey, step by step. Don't worry if you've never programmed before – we'll start from the very beginning and work our way up. So, grab a cup of your favorite beverage, and let's get started!

Batch Script - Variables

What Are Variables?

Before we jump into the specifics of Batch Script variables, let's understand what variables are in general. Think of variables as containers that hold information. Just like you might use a box to store your favorite toys, we use variables to store data in our programs.

Command Line Arguments

Let's start with something fun – command line arguments! These are like little messages we can send to our Batch Script when we run it.

Example 1: Hello, Name!

@echo off
echo Hello, %1!

Save this as greet.bat and run it like this: greet.bat John

What happens here? The %1 in our script is replaced by the first argument we provide (in this case, "John"). So, the script will say "Hello, John!"

Example 2: Multiple Arguments

@echo off
echo First argument: %1
echo Second argument: %2
echo Third argument: %3

Save this as args.bat and run it like this: args.bat apple banana cherry

This script will display:

First argument: apple
Second argument: banana
Third argument: cherry

Each %n (where n is a number) represents an argument in the order they're provided.

Set Command

The set command is our magic wand for creating variables in Batch Script. Let's see how it works!

Example 3: Creating a Simple Variable

@echo off
set message=Hello, World!
echo %message%

When you run this script, it will display "Hello, World!" The set command creates a variable named message and assigns it the value "Hello, World!". We then use %message% to display its contents.

Example 4: User Input

@echo off
set /p name=What's your name? 
echo Nice to meet you, %name%!

The /p flag with set allows us to prompt the user for input. This script asks for the user's name and then greets them.

Working with Numeric Values

Batch Script can handle numbers too! Let's explore some mathematical operations.

Example 5: Basic Arithmetic

@echo off
set /a result=5+3
echo 5 + 3 = %result%

set /a result=10-4
echo 10 - 4 = %result%

set /a result=6*2
echo 6 * 2 = %result%

set /a result=15/3
echo 15 / 3 = %result%

The /a flag tells set that we're dealing with arithmetic. This script demonstrates addition, subtraction, multiplication, and division.

Example 6: More Complex Calculations

@echo off
set /a result=(10+5)*2
echo (10 + 5) * 2 = %result%

set /a result=20%%3
echo 20 %% 3 = %result%

Here, we're using parentheses for order of operations and %% for modulus (remainder after division).

Local vs Global Variables

In Batch Script, variables are usually global, meaning they're accessible throughout the entire script. However, we can create local variables within blocks of code.

Example 7: Global vs Local Variables

@echo off
set global_var=I'm global!

setlocal
set local_var=I'm local!
echo Inside block: %local_var%
echo Global variable: %global_var%
endlocal

echo Outside block: %local_var%
echo Global variable: %global_var%

This script demonstrates how local variables are only accessible within their block (between setlocal and endlocal), while global variables can be accessed anywhere.

Working with Environment Variables

Environment variables are special variables that Windows uses to store system-wide information.

Example 8: Displaying Environment Variables

@echo off
echo Your username is: %USERNAME%
echo Your home directory is: %USERPROFILE%
echo The current date is: %DATE%
echo The current time is: %TIME%

This script displays some common environment variables. Windows provides many of these for us to use.

Example 9: Creating Custom Environment Variables

@echo off
setx MY_CUSTOM_VAR "Hello from the environment!"
echo %MY_CUSTOM_VAR%

The setx command creates a permanent environment variable. Note that you might need to open a new command prompt to see the changes.

Conclusion

Congratulations! You've just taken your first steps into the world of Batch Script variables. Remember, practice makes perfect, so don't be afraid to experiment with these examples and create your own scripts.

Here's a quick reference table of the commands we've learned:

Command Description
%n Access command line arguments
set Create or modify variables
set /p Create variables with user input
set /a Perform arithmetic operations
setlocal Start a local variable block
endlocal End a local variable block
setx Create environment variables

Happy scripting, and may your variables always be well-defined!

Credits: Image by storyset