This post is about how to use for loop in DOS programming.

There are two ways to declare and store the values in variables

  • Set Command
  • Command-line arguments

How to declare and use variables in batch programming using set command

variables are used to hold the values and strings. We can declare variables in batch programming using the SET keyword

here is an syntax

set [options] variable=value
  • set is a keyword
  • left side of = is treated as a variable and the right side of = is a value.
  • There is no space before or after = symbol
  • variable is a valid string in batch
  • value is a value stored and assigned to the variable
  • options are optional by default, /A and /P are some of the options.
  • Without options, variables are inferred as strings by default.

We can use for variable declaration with the following options

  • /P - Prompt the value to read from the command line
  • /A - treat the variable as numeric value

Once a variable is declared, you can use the variable with %variable% syntax.

echo "%variable%" 

The variable value is an echo to the command line.

Here is an example of declaring and using the variable with the set command

@echo off

set name = "john"
echo Hello, "%name%" How are you?


Hello, john How are you?

How to prompt to read the value from command line and store in a variable

/p option to set command, stop its execution and wait for the user to type string and read the value and store it in a variable.

@echo off
set /p name= Please enter name:
echo Hello, %name% How are you?

The output of the above code is

Please enter name: john
Hello,  john How are you?
Press any key to continue . . .

how to read and store numeric numbers in a variables batch programming?

/A option in the SET command allows you to treat the variable to store numeric values.

Here is an example to sum of the numbers and print the result to command line.

@echo off 
SET /A first = 10 
SET /A second = 20 
SET /A result = %first% + %second% 
echo %result%



How to read command line parameters in DOS batch programming?

Like any programming language, DOS batch enables to read command-line arguments using %1, %2, etc.

Here is an example to read command-line arguments and print to the command line.

@echo off 
echo %1 
echo %2
echo %3
echo %4

running job.bat one two three four command outputs the below


Variable types and scopes in Dos batch programming

scopes are variable scope exists in a period

Based on scopes, There are different variable types

  • Session and global variables

These variables exist in the running session of the dos window. Set command is an example of this.

set name=eric

the variable name only exists in the running session of the dos command prompt. The variable will not exist after the dos window is closed or session execution is closed in the current user context.

  • Machine level environment variables

These are system or machine levels. variables declared with this exists in multiple sessions and users. setx command is used to store the variable with values permanently.


setx variable value

Here is an example

setx JAVA_HOME "c:\jdk11"

The above JAVA_HOME environment variable exists after the Command line session is closed or logged with different users.

  • Local variables

local variables are used to the scope of the block. The variables declared with SETLOCAL and ENDLOCAL are called local variables.

@echo off 
set localname=Demo
echo %localname%