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 the set command
variables are used to hold the values and strings.
We can declare variables in batch programming using the
here is a syntax
set [options] variable=value
setis 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
variableis a valid string in batch
valueis 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.
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 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 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 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%
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
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.
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.
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
ENDLOCAL are called local variables.
@echo off SETLOCAL set localname=Demo echo %localname% ENDLOCAL