How variables are defined with the "set" command
In one sense, there are two categories of variables for the command line. Some might use the term "variable" for the placeholders or arguments %1, %2, ..%9, that are used to represent user input in batch files. (See the discussion on this page.) However, the term "variable" is normally reserved in command line usage for entities that are declared as environment variables with the "set" command. Note that this is a pretty primitive way to define variables. For example, there is no typing. Environment variables, including numbers, are stored as strings and operations with them have to take that into account. Variables are declared and given a value in a single statement using "set". .The syntax is: set some_variable = some_value
Variable names are not case-sensitve and can consist of the usual alphanumeric and other common characters. Some characters are reserved and have to be escaped. They should be avoided. These include the symbols in Table II on this page. Also, since these are environment variables, their names should be enclosed in percent signs when used in references and expressions, e.g, %some_variable%
. The percent signs are not used in the left side of the set statement that declares a variable.
Localizing variables
The declaration of a variable lasts as long as the present command window is open. If you are using a batch file that does not close its instance of the command window when the batch file terminates, any variables that the batch file declares remain. If you wish to localize a variable to a particular set of statements, use the "setlocal" and "endlocal" commands. Thus. to confine a variable declaration to a particular block of code, use:....
setlocal
set some_variable = some_value
...some statements
endlocal
...
Variables from user input
The "set" command can also accept input from a user as the value for a variable. The switch "/p" is used for this purpose. A batch file will wait for the user to enter a value after the statement set /p new_variable=
When the user has entered the value, the script will continue. A message string to prompt for input can also be used. For example:set /p new_variable="Enter value "
Note the space at the end of the prompt message. Otherwise, the prompt message and the user-entered value will run together on the screen. It works but it looks funny. The user may be tempted to hit the spacebar, which adds a leading space to the input value.
Arithmetic operations
The command line is not designed for handling mathematical functions but it is possible to do some very simple integer arithmetic with variables. A switch " /a" was added to the "set" command to allow for some basic functions. Primarily, the use is adding and subtracting. For example, it is possible to increment or decrement counters in a loop. In principle, it is also possible to do multiplication and division.but only whole numbers can be handled so the practical use is limited. Although variables are stored as strings, the command interpreter recognizes strings that contain only integers, allowing them to be used in arithmetic expressions. The syntax is set /a some_variable={arithmetic expression}
The four arithmetic operators are shown in Table I. (I have omitted a "modulus" operation, which uses the % sign in yet another way. In my opinion this just adds difficulty to an already quirky syntax. Using % in more than one sense can only confuse.)
Symbol | Operation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
Here is an example of a variable %counter% being incremented:set /a counter=%counter%+1
This can also be written as:set /a counter+=1
Variables in comparison statements in batch files
Variables are often used in comparisons in conditional statements in batch files. Some of the comparison operators that are used are given in Table I of the page on "If" statements. Because of the somewhat loose way that the command line treats variables, it is necessary to be careful when comparing variables. For strings, the safest way is to quote variables. For example: if "%variable1%" == "%variable2%" some_command