开始批处理文件中环境变量的本地化。本地化将持续到出现匹配的 endlocal 命令或者到达批处理文件结尾为止。
当您在脚本或批处理文件外使用 setlocal 时,将没有效果。
运行批处理文件时使用 setlocal 更改环境变量。运行 setlocal 后对环境所作的更改在批处理文件本地。Cmd.exe 在遇到 endlocal 命令或者到达批处理文件的结尾时将恢复上一次的设置。
setlocal 命令设置 ERRORLEVEL 变量。如果遇到 {enableextension | disableextensions} 或 {enabledelayedexpansion | disabledelayedexpansion},ERRORLEVEL 变量将设置成零 (0)。否则,该变量将被设置成 1。在批处理脚本中使用该命令可以确定扩展是否可用,例如:
verify other 2>nul
setlocal enableextensions
if errorlevel 1 echo Unable to enable extensions
因为当禁用命令扩展时 cmd 不会设置 ERRORLEVEL 变量,所以当通过无效参数使用 setlocal 命令时,verify 命令将 ERRORLEVEL 变量初始化为非零值。另外,如果通过 {enableextension | disableextensions 或 {enabledelayedexpansion | disabledelayedexpansion 参数使用 setlocal 命令,而且没有将 ERRORLEVEL 变量设置成一 (1) 时,则命令扩展将不可用。
有关启用和禁用命令扩展的详细信息,请参阅“相关主题”中的 cmd。
可以在批处理文件中本地化环境变量,如下所示:
rem *******Begin Comment**************
rem This program starts the superapp batch program on the network,
rem directs the output to a file, and displays the file
rem in Notepad.
rem *******End Comment**************
@echo off
setlocal
path=g:\programs\superapp;%path%
call superapp>c:\superapp.out
endlocal
start notepad c:\superapp.out
相关文章