Home > Commands N-Z > Commands S

SET


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

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.


Syntax

SET /?

SET [[/A] | [/P]] variable=[string]

Clear current environment setting:

SET variable=

Display current environment settings:

SET


Parameters
string (v2.0 Win95 NT3.5)
Specifies the string you want to associate with the specified variable.
variable (v2.0 Win95 NT3.5)
Specifies the variable you want to set or modify. Variables are not case sensitive. Variables can contain spaces.

Switches
/? (NTXP)
Displays help.
/A (NT4)
Arithmetic expressions, the expression to be evaluated can include the operators:
      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)
/P (NT2000)
Set a variable equal to a line of input entered by the user. The string is displayed before the user input is read. The string can be empty.

Related

For information about setting environment variables that the operating system uses to control its own operations, see the PATH, PROMPT, SHELL, and DIR commands.
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.


Notes
Displaying the current environment settings

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.


Using parameters

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).


Using SET in batch files

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.


Calling a SET variable from a batch file

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.


Effect of SET on environment space

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.


Using the %COPYCMD% environment variable

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.


Using the %NO_SEP% environment variable

UNDOCUMENTED SET NO_SEP=1 Removes the commas from numbers beginning with v6.2.

SET NO_SEP= to turn commas back on.


Environment variable substitution has been enhanced

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":

    %PATH:str1=str2%

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:

    %PATH:~10,5%

Extracts the last 10 characters of the %PATH% variable:

    %PATH:~-10%

Extracts all but the last 2 characters of the %PATH% variable:

    %PATH:~0,-2%

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%

Examples

To set an environment variable named %INCLUDE% so that the string C:\INC (the INC directory on drive C) is associated with it, type:

    SET INCLUDE=C:\INC

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:

    DIR %include%

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

and others:

    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

Errorlevels

none.


Availability
Internal
DOS
v2.0 v2.01 v2.05 v2.10 v2.11 v2.11R v2.12 v2.2 v2.25 v3.0 v3.20 v3.05 v3.1 v3.21 v3.25 v3.30 v3.3A v3.3R v3.3T v3.31 v3.40 v4.0 v4.01 v4.01A v5.0 v5.0A v5.00.02 v5.001A v5.01 v5.02 v6.0 v6.10 v6.2 v6.21 v6.22 v6.23 v7.00 v7.0R1 v7.10 v8.00
Windows
Win95 Win98 WinME
Windows NT
NT3.5 NT4 NT2000 NTXP NT2003