Display, set, or remove CMD environment variables. Environment variables allow you to store values (numbers or text) for later use, variable names are not case sensitive but the contents can be. Variables can contain spaces.
syntax SET variable SET variable=string SET /A variable=expression SET variable= SET /P variable=[promptString] key variable : A new or existing environment variable name string : A text string to assign to the variable. expression: : Arithmetic Sum
Always start variable names with a character i.e %v_myvar%
rather than %123_myvar%
This is to avoid the variable being mis-interpreted as a parameter [%1] [23_myvar]
Arithmetic expressions (SET /a)
The expression to be evaluated can include the following operators:
Multiply * Divide / Add + Subtract - Modulus % AND & OR | XOR ^ LSH << RSH >> Multiply Variable *= Divide Variable /= Add Variable += Subtract Variable -= AND Variable &= OR Variable |= XOR Variable ^= LSH Variable <<= RSH Variable <<=
New syntax in Windows 2000 only
SET /P variable=[promptString]
The /P switch allows you to set a variable equal to a line of
input entered by the user.
The promptString is displayed before the user input is read. The promptString can be empty.
Storing a text string:
SET v_department=Sales Department
Storing the value of another variable:
As Herbert Kleebauer pointed out in a recent post, SET can be CALLed allowing a variable substring to be evaluated.
SET start_char=10 SET length=9 SET string=The quick brown fox jumps over the lazy dog CALL SET substring=%%string:~%start_char%,%length%%% ECHO (%substring%)
Type SET without parameters to display all the current environment variables.
Type SET with just a variable name to display that variable
Alternatively use the ECHO command:
The SET command invoked with a string (and no equal sign) will display a wildcard list of all matching variables
will display all variables that begin with the letter 'P'
Deleting an environment variable
Type SET with just a variable name and an equals sign
The SET string can include any combination of text and other variables - they will all be concatenated into the new variable.
For example if we have
%v_part1%=first %v_part2%=second SET v_demo=%v_part1% %v_part2%
Then the variable will contain: first second
When "&" is used to put several commands on one line, the command processor will evaluate all the variables before executing any of the commands - so the script below will echo the word "first"
@ECHO OFF SET v_demo=first SET v_demo=second & ECHO %v_demo%
This quirk is frequently used with SETLOCAL
and ENDLOCAL to return variables
Using Variables in a calculation
Enclose any logical expressions in "quotes"
Several calculations can be put on one line if separated with commas.
Any SET /A calculation that returns a fractional result will be rounded down to the nearest whole number.
SET /A v_result=2+4 (=6) set /a v_result=2+4, v_amount -= 20 SET /A v_result="2<<3" (=2 Lsh 3 = binary 10 Lsh 3 = binary 10000 = decimal 16) SET /A v_result=5 %% 2 (=5/2 = 2 + 2 remainder 1 = 1) SET /A v_result=5 (=5) SET /A v_result+=5 (=10) SET /A v_result+=5 (=15) SET /A v_result=7 && 6 (=binary 111 AND binary 110 = binary 110 = 6)
SET /A will treat any character string in the expression
as an environment variable name. This allows you to do arithmetic with environment
variable values without having to type any % signs to get the values.
SET /A v_result=5 + NUMBER_OF_PROCESSORS
:: this will return 6
SET /A v_result="NUMBER_OF_PROCESSORS + 5"
:: this will return 6
SET /A v_result="5 + NUMBER_OF_PROCESSORS"
:: this will return 5
This last result demonstrates a minor bug present in NT 4 sp3.
Leading Zero will specify Octal
Numeric values are decimal numbers, unless prefixed by
0x for hexadecimal numbers,
0b for binary numbers and
0 for octal numbers.
So 0x12 is the same as 0b10010 is the same as 022.
The octal notation can be confusing - all numeric values that start with zeros are treated as octal but 08 and 09 are not valid numbers because 8 and 9 are not valid octal digits.
This is often a cause of error when performing date arithmetic. For example SET /a v_day=07 will return the value=7, but SET /a v_day=09 will return an error.
Changes made using the SET command are NOT permanent, they apply to the current CMD prompt only and remain only until the CMD window is closed.
To permanently change a variable at the command line use SETX
To permanently change a variable from the NT 4 GUI use
Control Panel, System, Environment, System Variables
Control Panel, System, Environment, User Variables
The "environment" dialogue box in NT 4.0 is notoriously confusing, you have to select a variable before you can add any new variables of the same type, if you don't have administrator rights then the edit option for System Variables is hidden.
Changing a variable permanently with SETX will not affect any CMD prompt that is already open.
Only new CMD prompts will get the new setting.
You can of course use SET to make immediate changes to the current CMD session, but neither SET or SETX will affect other CMD sessions that are already running. When you think about it - this is a good thing.
It is also possible (although undocumented) to add permanent env variables to the registry [HKEY_CURRENT_USER\Environment] with
REGEDIT /s MyRegScript.REG
System Environment variables can also be found in [HKLM\SYSTEM\CurrentControlSet\Control\Session
Any SET statement in c:\autoexec.bat may be parsed at boot time
Variables set in this way are not available to 32 bit gui programs - they won't appear in the control panel.
They will appear at the CMD prompt.
If autoexec.bat CALLS any secondary batch files, the additional batch files will NOT be parsed at boot.
This behaviour can be useful on a dual boot PC.
Variable names can include Spaces
A variable can contain spaces and also the variable name itself may contain spaces, therefore the following assignment:
SET v_variable =MyText
will create a variable called "v_variable "
To avoid problems with extra spaces appearing in your output, issue SET statements in parentheses, like this
(SET v_department=Some Text)
Alternatively you can do
SET "v_department=Some Text"
Note: if you wanted to actually include a bracket in the variable you need to use an escape character.
The SET command will set ERRORLEVEL to 1 if the variable name is not
found in the current environment.
This can be detected using the IF ERRORLEVEL command
If Command Extensions are disabled all
SET commands are disabled other than simple assignments like:
# I got my mind set on you
# I got my mind set on you
# I got my mind set on you... - George Harrison
SETX - Set an environment variable permanently.
SETLOCAL - Begin localisation of environment variable changes
ENDLOCAL - End localisation of environment changes
Parameters - get a full or partial pathname from a command line variable.
PATH - Change the %PATH% environment variable.
PATHMAN - This Resource Kit utility allows quick modification of both the system and user paths. Pathman can resolve many problems such as duplicate characters, and can improve performance by removing duplicate paths. For details see Pathman.wri in the resource kit.
REGEDIT - Import or export registry settings
Equivalent Linux BASH commands:
env - Display, set, or remove environment variables
export - Set an environment variable
set - Manipulate shell variables and functions