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 (so it's not really that similar to Option Explicit at all!)
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
This is an often misunderstood term, I would have
prefered it be called something like
'Expand_Variables_Inside_FOR_Loop'
To explain - when using any kind of FOR loop this is the default behaviour:
@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! inside the FOR loop.
EnableDelayedExpansion is Disabled by default.
EnableDelayedExpansion may also be enabled by starting CMD with the /v switch.
In some cases it can be helpful to use EnableDelayedExpansion outside a FOR loop: when combining or concatenating variables, the delimiters may be confused, by using EnableDelayedExpansion with the ! and % delimiters this can be avoided.
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 Commands:
ENDLOCAL - End localisation of environment changes
in a batch file.
Equivalent Linux BASH commands:
readonly - Mark variables/functions
as readonly