SoFunction
Updated on 2025-04-09

How to declare variables under DOS (define variables)

Dos define variables. Only environment variables can be used under DOS.

   SET [variable=[string]]  

variable   Specify the environment variable name.
string Specifies a series of strings to assign to a variable.

To display the current environment variable, type SET without parameters.

If the command extension is enabled, SET will change as follows:

The SET command can be activated with only one variable, and the equal sign or value does not show all prefix matches.
The value of all variables of the name that the SET command has used. For example:

     SET P  

All variables headed by the letter P will be displayed

If the variable name cannot be found in the current environment, the SET command will ERRORLEVEL
Set to 1.

The SET command does not allow variable names to contain equal signs.

 
Two new command line switches were added to the SET command:

     SET /A expression  
     SET /P variable=[promptString]  

The /A command line switch specifies that the string to the right of the equal sign is the numeric expression to be evaluated. This expression
The evaluator is simple and supports the following operations in decreasing order of priority:

()
! ~ -       - Univariate operator
*/ %        - Arithmetic operator
+ -         -Arithmetic Operator
<< >>         - Logical shift
&          -Bitwise “Ad”
^           -Bitwise “Extra”
|          -Bitwise “or”
= *= /= %= += -=   - Assignment
      &= ^= |= <<= >>=  
,              -Expression Separator

If you use any logic or remnant operators, you need to use the expression string with
quotation marks are expanded. Any non-numeric string key in the expression is used as an environment variable
Name, the values ​​of these environment variable names have been converted to numbers before use. If specified
Name, the values ​​of these environment variable names have been converted to numbers before use. If specified
An environment variable name is set but not defined in the current environment, then the value will be set as
zero. This allows you to use environment variable values ​​to do calculations without typing those % symbols
to get their values. If SET /A is executed on the command line outside the command script,
Then it displays the last value of the expression. The assigned operator is in the assigned operator
An environment variable name is required on the left. Hexadecimal has 0x prefix, octal
If prefixed with 0, the numeric value is a decimal number. Therefore, 0x12 is with 18 and 022
same. Please note that octal formulas may be easily confused: 08 and 09 are invalid numbers,
Because 8 and 9 are not valid octal digits.

The /P command line switch allows setting the variable value to a line input by the user. Read input
Before the line, the specified promptString is displayed. promptString can be empty.

Environment variable replacement has been enhanced as follows:

     %PATH:str1=str2%  

The PATH environment variable will be expanded, using "str2" instead of each "str1" in the extension result.
To effectively remove all "str1" from the extension result, "str2" can be empty.
"str1" can be started with an asterisk; in this case, "str1" will extend the result from
Starting from the first time the remaining part of str1 appears, it keeps matching.

You can also specify substrings for extensions.

     %PATH:~10,5%  

The PATH environment variable will be expanded and then used only from the 11th (partial) in the extension result
Movement 10) Five characters starting with characters. If no length is specified, the default is used
Value, that is, the remainder of the numeric value of the variable. If both numbers (offset and length) are negative,
The number used is the length of the environment variable value plus the specified offset or length.

     %PATH:~-10%  

The last ten characters of the PATH variable will be extracted.

     %PATH:~0,-2%  

Will extract all characters of the PATH variable except the last two.

Finally, support for delayed environment variable expansion has been added. This support is always based on the default value.
Deactivated, but can also be enabled/deactivated through the /V command line switch.
Please see CMD /?

Delaying environments considering the current expansion limitation encountered when reading a line of text
Variable expansion is very useful, not when executing. The following examples illustrate direct
The issue of variable expansion: