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 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

Assigning a numeric value

Getting value from a user




You can see, how to create batch file.

Let see few examples,

In 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.

batch variables

 

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

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

 

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

batch script numeric variable

 

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

var batch



Batch program to add two numbers

add batch var

Working of the above program

  • I have created two variable 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 below batch script, I am creating two variable one is global (var1) and another one is local (var2).

global script

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





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

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 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 argument and displaying it on the console using the echo.

command line argument batch file



Leave a Reply