Wednesday, 18 June 2014

setlocal command

About setlocal

Enables local environments to be changed without affecting anything else.

Availability

The setlocal command is an internal command that is available in the below Microsoft operating systems.

Windows NT 
Windows 2000
Windows XP
Windows Vista
Windows 7

Syntax

setlocal {enableextensions | disableextensions} {enabledelayedexpansion | disabledelayedexpansion}

Begins localization of environment changes in a batch file. Environment changes made after SETLOCAL has been 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 : Enables the command extensions until the matching endlocal command is encountered, regardless of the setting prior to the setlocal command.

disableextensions : Disables the command extensions until the matching endlocal command is encountered, regardless of the setting prior to the setlocal command.

enabledelayedexpansion : Enables the delayed environment variable expansion until the matching endlocal command is encountered, regardless of the setting prior to the setlocal command.

disabledelayedexpansion : Disables the delayed environment variable expansion until the matching endlocal command is encountered, regardless of the setting prior to the setlocal command.


 

Using setlocal 

When you use setlocal outside of a script or batch file, it has no effect.

Changing environmental variables

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. Cmd.exe restores previous settings when it either 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).

Testing for command extensions in batch files

The setlocal command sets the ERRORLEVEL variable. If you pass either {enableextensions | disableextensions} or {enabledelayedexpansion | disabledelayedexpansion}, the ERRORLEVEL variable is set to zero (0). Otherwise, it is set to one (1). You can use this in batch scripts to determine whether the extensions are available, for 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 one (1), command extensions are not available.

Reference:

http://technet.microsoft.com/en-us/library/bb491001.aspx

No comments:

Post a Comment