開始批處理文件中環(huán)境變量的本地化。本地化將持續(xù)到出現(xiàn)匹配的 endlocal 命令或者到達批處理文件結(jié)尾為止。
當(dāng)您在腳本或批處理文件外使用 setlocal 時,將沒有效果。
運行批處理文件時使用 setlocal 更改環(huán)境變量。運行 setlocal 后對環(huán)境所作的更改在批處理文件本地。Cmd.exe 在遇到 endlocal 命令或者到達批處理文件的結(jié)尾時將恢復(fù)上一次的設(shè)置。
setlocal 命令設(shè)置 ERRORLEVEL 變量。如果遇到 {enableextension | disableextensions} 或 {enabledelayedexpansion | disabledelayedexpansion},ERRORLEVEL 變量將設(shè)置成零 (0)。否則,該變量將被設(shè)置成 1。在批處理腳本中使用該命令可以確定擴展是否可用,例如:
verify other 2>nul
setlocal enableextensions
if errorlevel 1 echo Unable to enable extensions
因為當(dāng)禁用命令擴展時 cmd 不會設(shè)置 ERRORLEVEL 變量,所以當(dāng)通過無效參數(shù)使用 setlocal 命令時,verify 命令將 ERRORLEVEL 變量初始化為非零值。另外,如果通過 {enableextension | disableextensions 或 {enabledelayedexpansion | disabledelayedexpansion 參數(shù)使用 setlocal 命令,而且沒有將 ERRORLEVEL 變量設(shè)置成一 (1) 時,則命令擴展將不可用。
有關(guān)啟用和禁用命令擴展的詳細(xì)信息,請參閱“相關(guān)主題”中的 cmd。
可以在批處理文件中本地化環(huán)境變量,如下所示:
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
相關(guān)文章