Displays, sets, or removes the operating system environment variables.
You use environment variables to control the behavior of some batch files and programs and to control the way the operating system appears and works. The SET command is often used in the AUTOEXEC.BAT or CONFIG.SYS files to set environment variables each time you start the operating system.
SET [[/A] | [/P]] variable=[string]
Clear current environment setting:
Display current environment settings:
Grouping () NT2000 Separator , NT2000 Multiply * Divide / Add + Subtract - Modulus % AND & OR | XOR ^ LSH << RSH >> Multiply Variable *= Divide Variable /= Add Variable += Subtract Variable -= Modulus Variable %= NT2000 AND Variable &= OR Variable |= XOR Variable ^= LSH Variable <<= RSH Variable >>= octal 0 hexadecimal 0x binary 0b (0x12 == 0b10010 == 022 == 18)
For information about setting environment variables that the
operating system uses to control its own operations, see the
SETX - Set an environment variable permanently.
SETLOCAL - Begin localisation of environment variable changes.
ENDLOCAL - End localisation of environment changes.
Parameters - Get full or partial pathname from command-line 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.
Equivalent Linux BASH commands:
env - Display, set, or remove environment variables.
export - Set an environment variable.
set - Manipulate shell variables and functions.
When you type the SET command alone, the operating system displays the current environment settings. These settings usually include the %COMSPEC% and %PATH% environment variables that the operating system uses to help find programs on disk. %PROMPT%, %DIRCMD% and %COPYCMD% are some other environment variables that the operating system uses. For more information about %DIRCMD%, see the DIR command.
When you use a SET command and specify values for both variable and string, the operating system adds the specified variable value to the environment and associates the string with that variable. If the variable already exists in the environment, the new string value replaces the old string value.
If you specify only a variable and an equal sign (without a string) for the SET command, the operating system clears the string value associated with the variable (as if the variable is not there at all).
When creating batch files, you can use the SET command to create variables and use them in the same way as you would the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for the SET command.
When you call a variable value from a batch file, you must enclose the value with percent signs (%). For example, if your batch program creates an environment variable named BAUD, you can use the string associated with BAUD as a replaceable parameter by inserting %BAUD% on the command-line.
After you use a SET command, the operating system might display the message:
Out of environment space
This message means the available environment space is insufficient to hold the new variable definition. For information about how to increase the environment space, see the COMMAND command.
You can set the %COPYCMD% environment variable to specify whether you want the COPY, MOVE, and XCOPY commands to prompt you for confirmation before overwriting a file, whether issued from the command prompt or a batch file.
To force the COPY, MOVE, and XCOPY commands to prompt you before overwriting in all cases, set the %COPYCMD% environment variable to /-Y. To force these commands to overwrite in all cases without prompting you, set the %COPYCMD% environment variable to /Y.
Typing any of these commands with the /Y or /-Y switch overrides all defaults and the current setting of the %COPYCMD% environment variable.
UNDOCUMENTED SET NO_SEP=1 Removes the commas from numbers beginning with v6.2.
SET NO_SEP= to turn commas back on.
Beginning with Windows 2000 environment variable substitution has been enhanced:
Expands the %PATH% environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the begining of the expanded output to the first occurrence of the remaining portion of "str1":
You may also specify substrings for an expansion:
Expands the %PATH% environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified:
Extracts the last 10 characters of the %PATH% variable:
Extracts all but the last 2 characters of the %PATH% variable:
Finally, support for delayed environment variable expansion has been added. This support is always disabled by default, but may be enabled/disabled via the /V command-line switch to CMD.EXE.
Delayed environment variable expansion is useful for getting around the limitations of the current expansion which happens when a line of text is read, not when it is executed. This example demonstrates the problem with immediate variable expansion:
SET VAR=before IF "%VAR%" == "before" ( SET VAR=after; IF "%VAR%" == "after" @ECHO If you see this, it worked! )
Would never display the message, since the %VAR% in BOTH IF statements is substituted when the first IF statement is read, since it logically includes the body of the IF, which is a compound statement. So the IF inside the compound statement is really comparing "before" with "after" which will never be equal. Similarly, this example will not work as expected:
SET LIST= FOR %i IN (*) DO SET LIST=%LIST% %i ECHO %LIST%
In that it will NOT build up a list of files in the current directory, but instead will just set the %LIST% variable to the last file found. Again, this is because the %LIST% is expanded just once when the FOR statement is read, and at that time the LIST variable is empty. So the actual FOR loop we are executing is:
FOR %i IN (*) DO SET LIST= %i
Which just keeps setting LIST to the last file found.
Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time. If delayed variable expansion is enabled, the above examples could be written to work as intended:
SET VAR=before IF "%VAR%" == "before" ( SET VAR=after IF "!VAR!" == "after" @ECHO If you see this, it worked! ) SET LIST= FOR %i IN (*) DO SET LIST=!LIST! %i ECHO %LIST%
To set an environment variable named %INCLUDE% so that the string C:\INC (the INC directory on drive C) is associated with it, type:
You can then use the string C:\INC in batch files by enclosing the name INCLUDE with percent signs (%). For example, you might include the command in a batch file in order to display the contents of the directory associated with the %INCLUDE% environment variable:
When the operating system processes this command, the string C:\INC replaces %INCLUDE%.
Another possible use for the SET command is in a batch program that adds a new directory to the %PATH% environment variable:
@ECHO OFF REM ADDPATH.BAT adds a new directory REM to the %PATH% environment variable. SET path=%1;%path% SET
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%)
Display all variables that begin with the letter 'P':
CALL SET P
CALL SET /A v_result=2+4 :: this will return 6 CALL SET /A v_result=2+4 :: this will return 6 CALL SET /A v_result=2<<3 :: 2 Lsh 3 = binary 10 Lsh 3 = binary 10000 = decimal 16 CALL SET /A v_result=5 %% 2 :: 5/2 = 2 + 2 remainder 1 = 1 CALL SET /A v_result=5 :: 5 CALL SET /A v_result+=5 :: 10 CALL SET /A v_result+=5 :: 15 CALL SET /A v_result=7 && 6 :: binary 111 AND binary 110 = binary 110 = 6 CALL SET /A v_result=5 + NUMBER_OF_PROCESSORS :: 6 CALL SET /A v_result="NUMBER_OF_PROCESSORS + 5" :: 6 CALL SET /A v_day=1%v_day%-100 :: prevents 0 prefix being interpreted as octal