SETLOCAL

Set options to control the visibility of environment variables in a batch file.

Syntax
      SETLOCAL

      SETLOCAL EnableDelayedExpansion

      SETLOCAL EnableExtensions | DisableExtensions

SETLOCAL on it's own, usually at the start of a batch file, will begin localisation of Environment Variables. You might think of this as being vaguely analagous to Option Explicit in Visual Basic, however the script will still inherit all variables from the master environment/session and you will not be forced to define variables before using them.

Changes made to an Environment Variable after SETLOCAL has been issued are local to the batch file.

Issuing an ENDLOCAL command will restore the previous environment variables.

EnableDelayedExpansion

Normally batch files will expand environment variables once for each command/line. Also escaped characters (^) are evaluated just once. This can have undesirable side-effects when using commands which span multiple lines, like FOR and IF. Setting EnableDelayedExpansion will reverse this behaviour.

Examples

Escaping control characters:

@echo off
setlocal
Set _html=Hello^>World
Echo %_html%

In the above, the Echo command will create a text file called 'world' - not quite what we wanted! This is because the '^' caret works once for the SET command, but then vanishes.
If we now try the same thing with EnableDelayedExpansion, the caret works all the way through the script:

SETLOCAL EnableDelayedExpansion
Set _html=^<title^>Hello world ^</title^>
Echo !_html!
<title>Hello world </title>

With delayed expansion the caret ^ escapes each special character all the time, not just for one command.
This makes it possible to work with HTML and XML formatted strings in a variable.

Delayed variable expansion has a slightly different effect when working with Loops.
This is the default behaviour of a FOR loop:

@echo off
setlocal
:: count to 5 storing the results in a variable
set _tst=0
FOR /l %%G in (1,1,5) Do (echo [%_tst%] & set /a _tst+=1)
echo Total = %_tst%

C:\>demo_batch.cmd
[0]
[0]
[0]
[0]
[0]
Total = 5

Notice that when the FOR loop finishes we get the correct total, so the variable correctly increments, but during each iteration of the loop
the variable is stuck at it's initial value of 0

The same script with EnableDelayedExpansion, gives the same final result but also displays the intermediate values:

@echo off
setlocal EnableDelayedExpansion 
:: count to 5 storing the results in a variable
set _tst=0
FOR /l %%G in (1,1,5) Do (echo [!_tst!] & set /a _tst+=1)
echo Total = !_tst!
C:\>demo_batch.cmd
[0]
[1]
[2]
[3]
[4]
Total = 5

Notice that instead of %variable% we use !variable!

Example of replacing one variable with values from another:

@echo off
setlocal EnableDelayedExpansion
Set var1=Hello ABC how are you
Set var2=ABC
Set result=!var1:%var2%=Beautiful!
Echo [!result!]

An alternative method for achieving the above is CALL SET

EnableDelayedExpansion is Disabled by default.
EnableDelayedExpansion may also be enabled by starting CMD with the /v switch.

EnableDelayedExpansion can also be set in the registry:

[HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]
"DelayedExpansion"= (REG_DWORD)
1=enabled 0=disabled (default)

Overloading a variable

SETLOCAL can be used more than once in the same batch file so that multiple values can be stored in one Environment Variable.

@echo off
SETLOCAL
::Standard commission
SET _Commission=20
echo %_Commission%

::Premium commission
SETLOCAL
set _Commission=30
echo %_Commission%

::back to Standard commission
ENDLOCAL
echo %_Commission%


DISABLEEXTENSIONS

Command Extensions are enabled by default, DisableExtensions will attempt to disable Command extensions. (ENABLEEXTENSIONS - will attempt to re-enable)

SETLOCAL will set an ERRORLEVEL if given an argument. It will be zero if one of the two valid arguments is given and one otherwise.

You can use this in a batch file to determine if command extensions are available, using the following technique:

   VERIFY errors 2>nul
   SETLOCAL ENABLEEXTENSIONS
   IF ERRORLEVEL 1 echo Unable to enable extensions

This works because "VERIFY errors" sets ERRORLEVEL to 1 and then the SETLOCAL will fail to reset the ERRORLEVEL value if extensions are not available (e.g. if the script is running under command.com)

If Command Extensions are permanently disabled then SETLOCAL ENABLEEXTENSIONS will not restore them.

"A local shop for local people" - The League Of Gentlemen

Related:
ENDLOCAL - End localisation of environment changes in a batch file.
Syntax: Functions - How to package blocks of code
OldNewThing - Longer explanation of EnableDelayedExpansion
Powershell: Set-PSdebug -strict - Equivalent to 'Option Explicit' in VB
Equivalent bash command (Linux): readonly - Mark variables/functions as readonly



Back to the Top

Simon Sheppard
SS64.com