SET

(set)


显示、设置或删除 cmd.exe 环境变量。

 

SET [variable=[string]]

 

  variable  指定环境变量名。

  string    指定要指派给变量的一系列字符串。

 

要显示当前环境变量,键入不带参数的 SET

如果命令扩展名被启用,SET 会如下改变:

 

可仅用一个变量激活 SET 命令,等号或值不显示所有前缀匹配SET 命令已使用的名称的所有变量的值。例如:

 

    SET P

 

会显示所有以字母 P 打头的变量如果在当前环境中找不到该变量名称,SET 命令将把 ERRORLEVEL设置成 1

SET 命令不允许变量名含有等号。

 

SET 命令中添加了两个新命令行开关:

 

    SET /A expression

    SET /P variable=[promptString]

 

/A 命令行开关指定等号右边的字符串为被评估的数字表达式。该表达式评估器很简单并以递减的优先权顺序支持下列操作:

 

    ()                  - 分组

    ! ~ -               - 一元运算符

    * / %               - 算数运算符

    + -                 - 算数运算符

    << >>               - 逻辑移位

                       - 按位

    ^                   - 按位

    |                   - 按位

    = *= /= %= += -=    - 赋值

      &= ^= |= <<= >>=

    ,                   - 表达式分隔符

 

如果您使用任何逻辑或取余操作符, 您需要将表达式字符串用引号扩起来。在表达式中的任何非数字字符串键作为环境变量名称,这些环境变量名称的值已在使用前转换成数字。如果指定了一个环境变量名称,但未在当前环境中定义,那么值将被定为零。这使您可以使用环境变量值做计算而不用键入那些 % 符号来得到它们的值。如果 SET /A 在命令脚本外的命令行执行的,那么它显示该表达式的最后值。该分配的操作符在分配的操作符左边需要一个环境变量名称。除十六进制有 0x 前缀, 八进制有 0 前缀的,数字值为十进位数字。因此, 0x12 18 022相同。请注意八进制公式可能很容易搞混: 08 09 是无效的数字,因为 8 9 不是有效的八进制位数。

 

/P 命令行开关允许将变量数值设成用户输入的一行输入。读取输入行之前,显示指定的 promptStringpromptString 可以是空的。

 

环境变量替换已如下增强:

 

    %PATH:str1=str2%

 

会扩展 PATH 环境变量,用 "str2" 代替扩展结果中的每个 "str1"。要有效地从扩展结果中删除所有的 "str1""str2" 可以是空的。"str1" 可以以星号打头;在这种情况下,"str1" 会从扩展结果的开始到 str1 剩余部分第一次出现的地方,都一直保持相配。也可以为扩展名指定子字符串。

 

    %PATH:~10,5%

 

会扩展 PATH 环境变量,然后只使用在扩展结果中从第 11 (偏移量 10)字符开始的五个字符。如果没有指定长度,则采用默认值,即变量数值的余数。如果两个数字(偏移量和长度)都是负数,使用的数字则是环境变量数值长度加上指定的偏移量或长度。

 

    %PATH:~-10%

 

会提取 PATH 变量的最后十个字符。

 

    %PATH:~0,-2%

 

会提取 PATH 变量的所有字符,除了最后两个。

 

终于添加了延迟环境变量扩充的支持。该支持总是按默认值被停用,但也可以通过 CMD.EXE /V 命令行开关而被启用/停用。

请参阅 CMD /?

 

考虑到读取一行文本时所遇到的目前扩充的限制时,延迟环境变量扩充是很有用的,而不是执行的时候。以下例子说明直接变量扩充的问题:

 

    set VAR=before

    if "%VAR%" == "before" (

        set VAR=after

        if "%VAR%" == "after" @echo If you see this, it worked

    )

 

不会显示消息,因为在读到第一个 IF 语句时,BOTH IF 语句中的 %VAR% 会被代替;原因是: 它包含 IF 的文体,IF 是一个复合语句。所以,复合语句中的 IF 实际上是在比较 "before" "after",这两者永远不会相等。同样,以下这个例子也不会达到预期效果:

 

    set LIST=

    for %i in (*) do set LIST=%LIST% %i

    echo %LIST%

 

原因是,它不会在目前的目录中建立一个文件列表,而只是将LIST 变量设成找到的最后一个文件。这也是因为 %LIST% FOR 语句被读取时,只被扩充了一次;而且,那时的 LIST 变量是空的。因此,我们真正执行的 FOR 循环是:

 

    for %i in (*) do set LIST= %i

 

这个循环继续将 LIST 设成找到的最后一个文件。

 

延迟环境变量扩充允许您使用一个不同的字符(惊叹号)在执行时间扩充环境变量。如果延迟的变量扩充被启用,可以将上面例子写成以下所示,以达到预期效果:

 

    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%

 

如果命令扩展名被启用,有几个动态环境变量可以被扩展,但不会出现在 SET 显示的变量列表中。每次变量数值被扩展时,这些变量数值都会被动态计算。如果用户用这些名称中任何

一个定义变量,那个定义会替代下面描述的动态定义:

%CD% - 扩展到当前目录字符串。

%DATE% - 用跟 DATE 命令同样的格式扩展到当前日期。

%TIME% - 用跟 TIME 命令同样的格式扩展到当前时间。

%RANDOM% - 扩展到 0 32767 之间的任意十进制数字。

%ERRORLEVEL% - 扩展到当前 ERRORLEVEL 数值。

%CMDEXTVERSION% - 扩展到当前命令处理器扩展名版本号。

%CMDCMDLINE% - 扩展到调用命令处理器的原始命令行。

 

★★★★★实例★★★★★:

要设置名为INCLUDE的变量,使它等于字符串C:\INC(C盘中的INC目录),可键入以下命令:

set include=c:\inc

 

在批处理文件可用%INCLUDE%代替C:\INC.例如,可在批处理文件中包括以下命令,显示INCLUDE环境变量表示的目录内容:

dir %include%

MS-DOS处理该命令时,会用字符串C:\INC代替%INCLUDE%

 

SET命令在批处理程序中的另一种用法是在PATH环境变量中加入新目录,例如:

@echo off

rem ADDPATH.BAT adds a new directory

rem to the PATH environment variable

set path=%1;%path%

set 


Displays, sets, or removes cmd.exe environment variables.

 

SET [variable=[string]]

 

  variable  Specifies the environment-variable name.

  string    Specifies a series of characters to assign to the variable.

 

Type SET without parameters to display the current environment variables.

 

If Command Extensions are enabled SET changes as follows:

 

SET command invoked with just a variable name, no equal sign or value will display the value of all variables whose prefix matches the name given to the SET command.  For example:

 

    SET P

 

would display all variables that begin with the letter 'P'

 

SET command will set the ERRORLEVEL to 1 if the variable name is not found in the current environment.

 

SET command will not allow an equal sign to be part of the name of a variable.

 

Two new switches have been added to the SET command:

 

    SET /A expression

    SET /P variable=[promptString]

 

The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated.  The expression evaluator is pretty simple and supports the following operations, in decreasing order of precedence:

 

    ()                  - grouping

    ! ~ -               - unary operators

    * / %               - arithmetic operators

    + -                 - arithmetic operators

    << >>               - logical shift

    &                   - bitwise and

    ^                   - bitwise exclusive or

    |                   - bitwise or

    = *= /= %= += -=    - assignment

      &= ^= |= <<= >>=

    ,                   - expression separator

 

If you use any of the logical or modulus operators, you will need to enclose the expression string in quotes.  Any non-numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them.  If an environment variable name is specified but is not defined in the current environment, then a value of zero is used.  This allows you to do arithmetic with environment variable values without having to type all those % signs to get their values.  If SET /A is executed from the command line outside of a command script, then it displays the final value of the expression.  The assignment operator requires an environment variable name to the left of

the assignment operator.  Numeric values are decimal numbers, unless prefixed by 0x for hexadecimal numbers, and 0 for octal numbers. So 0x12 is the same as 18 is the same as 022. Please note that the octal notation can be confusing: 08 and 09 are not valid numbers because 8 and 9 are not valid octal digits.

 

The /P switch allows you to set the value of a variable to a line of input entered by the user.  Displays the specified promptString before reading the line of input.  The promptString can be empty.

 

Environment variable substitution has been enhanced as follows:

 

    %PATH:str1=str2%

 

would expand 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 beginning of the expanded output to the first

occurrence of the remaining portion of str1.

 

May also specify substrings for an expansion.

 

    %PATH:~10,5%

 

would expand 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%

 

would extract the last 10 characters of the PATH variable.

 

    %PATH:~0,-2%

 

would extract 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.  See CMD /?

 

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.  The following 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, the following 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 as follows 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%

 

If Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don't show up in the list of variables displayed by SET.  These variable values are computed dynamically each time the value of the variable is expanded.

If the user explicitly defines a variable with one of these names, then that definition will override the dynamic one described below:

 

%CD% - expands to the current directory string.

 

%DATE% - expands to current date using same format as DATE command.

 

%TIME% - expands to current time using same format as TIME command.

 

%RANDOM% - expands to a random decimal number between 0 and 32767.

 

%ERRORLEVEL% - expands to the current ERRORLEVEL value

 

%CMDEXTVERSION% - expands to the current Command Processor Extensions

    version number.

 

%CMDCMDLINE% - expands to the original command line that invoked the

    Command Processor.