SET
Displays, sets, or removes environment variables (of CMD.EXE).
SET [variable[=[value]]]
SET [letter]
SET [/A [variable=]"expression"]
SET [/P variable=[promptstring]]
Where:
variable
- Specifies the environment variable to give a value to (defines the specified variable). If neither=
norvalue
is specified, it will display the value of the environment variable (if defined). If=
is specified but notvalue
, it will remove the specified environment variable (if defined).value
- Specifies the value to give to the variable specified by defining it (the value is a word, phrase, number, or other).letter
- Displays the value of environment variables whose names begin with the specified letters./A
- Specifies that an arithmetic expression must be calculated. Ifvariable
is specified, the specified environment variable will be defined with the result of the expression."expression"
- Specifies the expression whose result will be displayed. If variable is specified, the specified environment variable will be defined with the result of the expression./P
- Specifies that the environment variable must be user-defined (i.e., anyone opening the batch file containing this command will have the option to write any string).promptstring
- Displays the specified character string which can be a question or a request.
What is the purpose of defining variables?
As explained above, enclosing the environment variable in percentage symbols % %
will expand its value (if defined, of course).
The following operators can be used in arithmetic expressions:
( )
- Grouping (brackets).! ~
- Logical and bitwise NOT operators (respectively).+
- Plus symbol (arithmetic addition).-
- Minus symbol (arithmetic subtraction).*
- Times symbol (arithmetic multiplication)./
- Divided-by symbol (arithmetic division).<< >>
- Bitwise shift.&
- Bitwise AND.^
- Bitwise XOR (exclusive or).|
- Bitwise OR.c=n
- Assignment (wherec
is any character other than a number or symbol among those specified andn
is a number or expression).,
- Expression separator (NB: if this character is specified one or more times, the last result will be used to set the environment variable).
Note that if no arguments are added to the SET
command, the complete list of defined environment variables will be displayed.
Examples:
1. Assign the FOO
value to the hugh
environment variable:
set hugh=FOO
2. Assign the value of the expression 15+18
(33) to the environment variable HOWMUCH
:
set /a HOWMUCH="15+18"
3. Return the value of the user's response to the CHOICE
environment variable by displaying Hi Foo, what is 1+1?
(see Batch filesBatch files):
set /p CHOICE=Hi Foo, what is 1+1?
Further information:
To expand an environment variable you need to put it between percentages % %
, for example %CHOICE%
, but for custom expansions there are different syntaxes:
%CHOICE:word1=word2%
This line expands the CHOICE
environment variable by replacing the words word1
with the words word2
. word2
may be left unspecified to remove the words word1
.
%CHOICE:~2,4%
Expands the first 4 characters starting from the third character (skipping the first 2 characters) of the CHOICE
environment variable. If 4 is not specified, it extracts the first 2 characters of the CHOICE
environment variable. If 0 is specified in the first number, it extracts all characters except the first 4.
%CHOICE:~-4%
Expands the last 4 characters of the CHOICE
environment variable.
%CHOICE:~0,-4%
Expands all characters except the last 4 of the CHOICE
environment variable.
Comments