Home XP Commands
XP Syntax

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.

Example: 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 escaping control characters:

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

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:

@echo off
setlocal EnableDelayedExpansion 
Set _MyCode=Hello^>World
Echo !foo!

This makes it possible to store HTML or XML strings in a variable: ^<tag^>this is some text^</tag^>

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

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.

For example:
@echo off
::
::Standard commission
SET _Commission=20
echo %_Commission%

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

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

::Back to Super commission
ENDLOCAL
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.
OldNewThing - Longer explanation of EnableDelayedExpansion

Equivalent Linux BASH commands:

readonly - Mark variables/functions as readonly



Back to the Top

Simon Sheppard
SS64.com