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.

If a batch script does not use SETLOCAL and ENDLOCAL then all variables will be Global - visible and modifiable by other scripts.

Although global variables are easy to work with they are not good practice - for example if you have several batch scripts dealing with filenames (and these scripts may be CALLing one another), the first script may have a variable called _filename, the second script a different variable called file-name (you choose a different name to avoid conflicting with the other script) a third script now needs something like file_name it quickly becomes very difficult to manage things like this.

With local variables you are free to standardise and use the same name in multiple batch scripts - it doesn't matter because they are not visible to any other script.
Local Variables can be passed from one batch routine to another with the ENDLOCAL command.

Issuing a SETLOCAL command, the batch script will inherit all current variables from the master environment/session.

Issuing an ENDLOCAL command will restore any environment variables present before the SETLOCAL was issued.

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

© Copyright SS64.com 1999-2010
Some rights reserved