Batch file variables and scope

batch file variables

The batch script also supports the concept of the variable similar to the other programming language. In this article, you will see how we can create a batch file variables and how we can use it.

In the batch file, we can create variable two types, one is using the set commands and another is for the parameters which can be passed when the batch file is called. First I will describe how we can create a variable using the set command.

You can see, Batch file commands

SET

If used set command without parameters, it displays the current environment variable settings. We can also create a variable or overwrite any existing variable using the set command.

Syntax,

Assigning a string

set Variable_Name=Variable_Value

 

Assigning a numeric value

set /a Variable_Name=Numeric_Value

 

Getting value from a user and assigning it to the variable.

set /p Variable_Name=Line_input_User

 

You can see, how to create a batch file.

Let see few examples,

In the bellow batch script, I am creating a variable Blog and storing a string Aticleworld. After storing the string I am displaying it on the console.

@echo OFF
SET Blog=Aticleworld
Rem ***displaying Blog name***
ECHO %Blog%

batch variables

 

There should not space between the variable name and the equal sign (=).

@echo OFF
SET Blog = Aticleworld
Rem ***displaying Blog name***
ECHO %Blog%

C:\Users\pc\Desktop\modify image\batch\var

 

In the bellow batch script, I am creating a variable year (numeric variable) to store the years and displaying the value on the console.

@echo OFF
SET /a year=1991
Rem ***displaying year***
ECHO %year%

batch script numeric variable

 

In the bellow batch script, I am creating a variable var to get the value from the user and displaying it on the console.

@echo OFF
echo Enter value
SET /p var=
echo value is
ECHO %var%

var batch

 

Batch program to add two batch file variables

 

@echo off 
SET /A a = 6 
SET /A b = 27 
SET /A c = %a% + %b% 
echo %c%

add batch var

Code Analysis:

  • I have created two variables a and b and store 6 and 27 respectively.
  • Adding the value of a,b, and store it in c.
  • Now, displaying the value of the c.

 

Variable Scope (Global vs Local)

By default the scope of the variable in a batch file is global. It means if you will create a variable in the batch file that it can be accessed anywhere in the program.

Local variables have a defined boundary in which only they can be accessed. In the batch script, we can create a local variable using the SETLOCAL command. The scope of the local variable only between the SETLOCAL and ENDLOCAL command and it is destroyed as soon as the ENDLOCAL statement is executed.

See the below example,

In the below batch script, I am creating two variable one is global (var1) and another one is local (var2).

@echo oFF
rem it a is global variable
SET var1=global 

rem it a is global variable
SETLOCAL
SET var2=local 

rem display local variable
ECHO %var2%  
ENDLOCAL

rem display global variable
ECHO %var1% 

PAUSE

global script

So you can see in this output, you able to access both local and global variables and echo is printing their value.

Now let’s see what happens when we try to use a local variable beyond its scope which means try to access the local variable after ENDLOCAL.

@echo oFF
rem it a is global variable
SET var1=global 

rem it a is global variable
SETLOCAL
SET var2=local 

ENDLOCAL

rem display local variable
ECHO %var2% 

rem display global variable
ECHO %var1% 

PAUSE

 

Access Local Beyond Scope

So you can see in this output when you try to access the local variable beyond its scope, the ECHO is turned off.

 

Command Line Arguments in Batch file

The batch file can read the command-line argument with a special syntax. If you want to read the command line arguments you need to write % with command line argument position. Suppose if you want to read the 1st argument of the command line, you need to write %1 in the batch file.

Let see example code,

Below batch file accept three command-line arguments and displaying it on the console using the echo.

@echo oFF

echo %1 
echo %2 
echo %3

PAUSE

 

command line argument batch file

 

Recommended Articles for you:



One comment

  1. “So you can see in this output when you try to access the local variable beyond its scope, the ECHO is turned off.”

    Um, no. since %var2% is not accessible, it is simply replaced by an empty string. so no arguments are sent to ECHO.

    In ECHO help it is written:
    Type ECHO without parameters to display the current echo setting.

    So since echo was turned OFF earlier in the script (using @echo oFF) it displays “ECHO is off.”

    ECHO %var2% didnt turn ECHO off; it was already off; it just displayed it.

Leave a Reply

Your email address will not be published. Required fields are marked *