命令行大全

set

嚯嚯 发表于 2020-06-28 18:26浏览次数:

set命令允许用户改变一个变量或串到另一个。

查看英文版

目录:

1 set 运行系统环境

2 set 语法

3 set 示例

4 隐藏命令

set 运行系统环境

Windows 95

Windows 98

Windows xp

Windows 2000

Windows 7

Windows 8

Windows 10

Windows NT

Windows ME

All Versions of MS-DOS

set 语法

Windows 2000,XP及更高版本的语法

显示,设置或删除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命令允许您将变量的值设置为用户输入的输入行。在读取输入行之前显示指定的hintString。promptString可以为空。

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

%PATH:str1=str2%

将扩展PATH环境变量,用str2替换扩展结果中str1的每次出现。str2可以是空字符串,以有效地从扩展输出中删除所有出现的str1str1可以以星号开头,该星号与从扩展输出的开始到str1其余部分的第一次出现的所有内容匹配。

也可以为扩展指定子字符串。

%PATH:~10,5%

将扩展PATH环境变量,然后仅使用从扩展结果的第11个(偏移10)字符开始的5个字符。如果未指定长度,则默认为变量值的其余部分。如果数字(偏移量或长度)为负,则使用的数字是环境变量值的长度加上指定的偏移量或长度。

%PATH:~-10%

将提取PATH变量的最后10个字符。

%PATH:~0,-2%

会提取PATH变量的最后两个字符(最后两个字符除外)。

最后,添加了对延迟的环境变量扩展的支持。默认情况下,始终禁用此支持,但是可以通过/ V命令行开关CMD.EXE启用/禁用此支持。看到CMD /?

延迟的环境变量扩展有助于避免当前扩展的局限性,当前局限性是在读取一行文本时而不是在执行文本行时发生的。下面的示例演示了立即变量扩展的问题:

set VAR=before
if "%VAR%" == "before" (
set VAR=after;
if "%VAR%" == "after" @echo If you see this, it worked
)

永远不会显示该消息,因为当将谎言读入内存时,%VAR%会展开。之后的变量所发生的变化(例如,复合if语句中的VAR = after)不会扩展。

同样,以下示例将无法正常工作:

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

因为它不会在当前目录中建立文件列表,而是将LIST变量设置为找到的最后一个文件。同样,这是因为读取FOR语句后,%LIST%会扩展,此时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% - 扩展到调用命令处理器的原始命令行。

Windows 2000和Windows XP故障恢复控制台语法

set [enviroment_variable]=[True/False]
[enviroment_variable] allowwildcards
allowallpaths
allowremovablemedia
nocopyprompt
[True/False] 将enviroment_variable设置为true可启用enviroment_variable。默认情况下,这些设置为False

MS-DOS,Windows 95,Windows 98,Windows ME语法

显示,设置或删除Windows环境变量。

SET [variable=[string]]
variable 指定环境变量名称。
string 指定要分配给变量的一系列字符。

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

Windows 2000, XP, and later syntax

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 a variable name, no equal sign or value displays 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 sets the ERRORLEVEL to 1 if the variable name is not found in the current environment.

SET command doesn't allow an equal sign to be part of the name of a variable.

Two new switches are 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
* / % 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 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. Doing 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. Note: 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 is enhanced as follows:

%PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence of str1 in the expanded result with str2str2 can be the empty string to effectively delete all occurrences of str1 from the expanded output. str1 can begin with an asterisk, which matches 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 was 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 to get around the limitations of current expansion that happens when reading a line of text, 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, because %VAR% is expanded when the lie is read into memory. Changes that occur to the variable after that (such as VAR=after in our compound if statement) is not expanded.

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 set the LIST variable to the last file found. Again, this is because the %LIST% expandeds once 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 keeps setting LIST to the last file found.

Delayed environment variable expansion allows 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 several dynamic environment variables can be expanded, which don't show up.

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 overrides the dynamic one described below:

%CD% - expands to the current directory string.

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

%TIME% - expands to the current time using the 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.

Windows 2000 and Windows XP Recovery Console syntax

set [enviroment_variable]=[True/False]
[enviroment_variable] allowwildcards
allowallpaths
allowremovablemedia
nocopyprompt
[True/False] Setting the enviroment_variable to true enables the enviroment_variable. By default, these are set to False.

MS-DOS, Windows 95, Windows 98, Windows ME syntax

Displays, sets, or removes Windows 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.

查看英文版

查看中文版

set 示例

set path=c:\windows\command

将路径设置为c:\ windows \ command。

set

显示当前定义的所有设置的环境变量。

如何在Windows 2000和Windows XP中使用set命令替代choice命令。

在下面的示例中,将提示用户输入1,2或3的选项以打印hellobyetest

@ECHO off
cls
:start
ECHO.
ECHO 1. Print Hello
ECHO 2. Print Bye
ECHO 3. Print Test
set choice=
set /p choice=Type the number to print text.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto hello
if '%choice%'=='2' goto bye
if '%choice%'=='3' goto test
ECHO "%choice%" is not valid, try again
ECHO.
goto start
:hello
ECHO HELLO
goto end
:bye
ECHO BYE
goto end
:test
ECHO TEST
goto end
:end
set path=c:\windows\command

Set the path to c:\windows\command.

set

Display all the set environment variables currently defined.

How to use the set command as a substitute for the choice command in Windows 2000 and Windows XP

In the example below, a user would be prompted to enter an option of 1,2, or 3 to print hellobye, or test.

@ECHO off
cls
:start
ECHO.
ECHO 1. Print Hello
ECHO 2. Print Bye
ECHO 3. Print Test
set choice=
set /p choice=Type the number to print text.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto hello
if '%choice%'=='2' goto bye
if '%choice%'=='3' goto test
ECHO "%choice%" is not valid, try again
ECHO.
goto start
:hello
ECHO HELLO
goto end
:bye
ECHO BYE
goto end
:test
ECHO TEST
goto end
:end

Microsoft Windows 2000和Windows XP中的用户可能难以通过MS-DOS提示符定义设置值。

Users in Microsoft Windows 2000 and Windows XP may have difficulty defining the set values through the MS-DOS prompt.

查看英文版

查看中文版

隐藏命令

如果要对用户隐藏所有目录,则可以使用:

SET DIRCMD=0

上面的命令可以防止任何人看到目录。但是,仍然可以访问它们。要使目录再次可见,请键入以下命令。

SET DIRCMD=

If you wanted to hide all your directories from users, you could use:

SET DIRCMD=0

The above command prevents anyone from seeing the directories; however, they can still be accessed. To allow the directories to be visible again, type the command below.

SET DIRCMD=

查看英文版

查看中文版