setlocal (更改本地环境)

嚯嚯 发表于 2020-06-28 16:49
浏览次数:
在手机上阅读

通过setlocal命令,可以更改本地环境,而不会影响任何其他内容。

查看英文版

目录

1 setlocal 运行系统环境

2 setlocal 语法

3 setlocal 示例

setlocal 运行系统环境

Windows xp

Windows vista

Windows 2000

Windows 7

Windows 8

Windows 10

MS-DOS 5.0 及以上版本

Windows NT

setlocal 语法

开始在批处理文件中本地化环境更改。发出SETLOCAL后所做的环境更改在批处理文件中是本地的。必须发出ENDLOCAL才能恢复以前的设置。到达批处理脚本的末尾时,将对该批处理脚本发出的所有未完成的SETLOCAL命令执行隐式ENDLOCAL。

SETLOCAL

如果启用了命令扩展,则SETLOCAL更改如下:

SETLOCAL批处理命令现在接受可选参数:

ENABLEEXTENSIONS /
DISABLEEXTENSIONS
启用或禁用命令处理器扩展。有关  详细信息,请参见CMD
ENABLEDELAYEDEXPANSION /
DISABLEDELAYEDEXPANSION
启用或禁用延迟的环境变量扩展。看到SET /?有关详细信息。

这些修改将持续到匹配的ENDLOCAL命令为止,无论它们在SETLOCAL命令之前的设置如何。

如果给定参数,SETLOCAL命令将设置ERRORLEVEL值。如果给出两个有效参数之一,则为零;否则,为零。您可以使用以下技术在批处理脚本中使用它来确定扩展是否可用:

VERIFY OTHER 2>nul
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 echo Unable to enable extensions

上面的示例之所以有效,是因为在旧版本的CMD.EXE中,SETLOCAL不会设置ERRORLEVEL值。带有错误参数的VERIFY命令会将ERRORLEVEL值初始化为非零值。

Begins localization of environment changes in a batch file. Environment changes made after SETLOCAL is issued are local to the batch file. ENDLOCAL must be issued to restore the previous settings. When the end of a batch script is reached, an implied ENDLOCAL is executed for any outstanding SETLOCAL commands issued by that batch script.

SETLOCAL

If Command Extensions are enabled SETLOCAL changes as follows:

SETLOCAL batch command now accepts optional arguments:

ENABLEEXTENSIONS /
DISABLEEXTENSIONS
Enable or disable command processor extensions. See CMD  for details.
ENABLEDELAYEDEXPANSION /
DISABLEDELAYEDEXPANSION
Enable or disable delayed environment variable expansion. See SET /? for details.

These modifications last until the matching ENDLOCAL command, regardless of their settings before the SETLOCAL command.

The SETLOCAL command sets the ERRORLEVEL value if given an argument. It will be zero if one of the two valid arguments is given and one otherwise. You can use this in batch scripts to determine if the extensions are available, using the following technique:

VERIFY OTHER 2>nul
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 echo Unable to enable extensions

The above example works because on old versions of CMD.EXE, SETLOCAL does NOT set the ERRORLEVEL value. The VERIFY command with a bad argument initializes the ERRORLEVEL value to a non-zero value.

查看英文版

查看中文版

setlocal 示例

SETLOCAL
在批处理文件中运行时,setlocal命令将仅在批处理文件中影响所有环境更改。
SETLOCAL
Ran in a batch file, the setlocal command would have all environment changes only affected in the batch file.

另请参见语法中显示的Microsoft上面的示例,以了解可以使用此命令的其他方式

Also see Microsoft's above example shown in the syntax for other ways this command can be used.

查看英文版

查看中文版

其他命令行

systemroot | systeminfo | sys | switches | sc | subst | start | sort | smartdrv | shutdown | shift | share | sfc | setver | set | scandisk | scanreg | schtasks |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条