Setlocal


Applies To: Windows Server 2008,Windows Vista

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.

For examples of how to use this command, see Examples.

Syntax


Copy Code

setlocal [enableextensions | disableextensions] [enabledelayedexpansion | disabledelayedexpansion]

Arguments

Argument

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


Copy Code

setlocal enableextensions
verify other 2>nul
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

You can localize environment variables in a batch file, as shown in the following sample script:


Copy Code

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

Additional references

Command-Line Syntax Key