大网数据广告活动
Windows排障命令

setlocal

· 52CE 我爱测

Reference article for the setlocal command, which starts localization of environment variables in a batch file.

Starts localization of environment variables in a batch file. Localization continues until a matching endlocal command is encountered or the end of the batch file is reached.

Syntax

setlocal [enableextensions | disableextensions] [enabledelayedexpansion | disabledelayedexpansion]

Parameters

Parameter Description
enableextensions Enables the command extensions until the matching endlocal command is encountered, regardless of the setting before the setlocal command was run.
disableextensions Disables the command extensions until the matching endlocal command is encountered, regardless of the setting before the setlocal command was run.
enabledelayedexpansion Enables the delayed environment variable expansion until the matching endlocal command is encountered, regardless of the setting before the setlocal command was run.
disabledelayedexpansion Disables the delayed environment variable expansion until the matching endlocal command is encountered, regardless of the setting before the setlocal command was run.
/? Displays help at the command prompt.
Remarks
  • If you use setlocal outside of a script or batch file, it has no effect.

  • Use setlocal to change environment variables when you run a batch file. Environment changes made after you run setlocal are local to the batch file. The Cmd.exe program restores previous settings when it encounters an endlocal command or reaches the end of the batch file.

  • You can have more than one setlocal or endlocal command in a batch program (that is, nested commands).

  • The setlocal command sets the ERRORLEVEL variable. If you pass {enableextensions | disableextensions} or {enabledelayedexpansion | disabledelayedexpansion}, the ERRORLEVEL variable is set to 0 (zero). Otherwise, it's set to 1. You can use this information in batch scripts to determine whether the extensions are available, as shown in the following example:

    verify other 2>nul
    setlocal enableextensions
    if errorlevel 1 echo Unable to enable extensions
    

    Because cmd does not set the ERRORLEVEL variable when command extensions are disabled, the verify command initializes the ERRORLEVEL variable to a nonzero value when you use it with an invalid argument. Also, if you use the setlocal command with arguments {enableextensions | disableextensions} or {enabledelayedexpansion | disabledelayedexpansion} and it does not set the ERRORLEVEL variable to 1, command extensions are not available.

Examples

To localize environment variables in a batch file, follow this sample script:

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

Related links

Source: Microsoft Learn / Windows Server documentation by Microsoft and contributors (upstream author metadata: robinharwood). Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Server documentation
适用范围
Windows Server · Source date 10/16/2017
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。