This post talks about complete tutorials in Variables on DOS programming.

variables in DOS programming allows you

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 the set command

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

here is a 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 the = 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 it for variable declaration with the following options.

  • /P - Prompt the value to read from the command line
  • /A - treat the variable as a 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?

Output:

Hello, john How are you?

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

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

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

The output of the above code is

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

How to read and store numeric numbers in variable batch programming?

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

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

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

Output:

30

How to read command line parameters in DOS batch programming?

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

Here is an example to read command-line arguments and printing 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

one
two
three
four

Variable types and scopes in DOS batch programming

scopes are variable scope that 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 exist in multiple sessions and users. setx command is used to store the variable with values permanently.

Syntax

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
SETLOCAL
set localname=Demo
echo %localname%
ENDLOCAL

Output:

Demo