This post discusses comprehensive tutorials on Variables in DOS programming.

Variables in DOS programming allow you to store the values in a variable

There are two ways to declare and store 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 values and strings. We can declare variables in batch programming using the SET keyword.

Here is the syntax:

set [options] variable=value
  • set is a keyword.
  • The 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 the 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 echoed 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

The /P option in the set command stops its execution and waits 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?

The /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 the reading of 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:

one
two
three
four

Variable Types and Scopes in DOS Batch Programming

Scopes are variable scopes that exist 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 in with different users.

  • Local Variables Local variables are used in 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