开始在批处理文件中本地化环境更改。发出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.