fortypoundhead.com

SETLOCAL

Posted On 2007-04-24 by FortyPoundHead
Keywords: Command Reference
Tags: Windows Commandline Windows
Views: 1658


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

Syntax
SETLOCAL

SETLOCAL EnableDelayedExpansion

SETLOCAL EnableExtensions | DisableExtensionsSETLOCAL 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 = 5Notice 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 = 5Notice 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 extensionsThis 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.


About the Author

FortyPoundHead has posted a total of 1974 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.82.56.95

Before you can post, you need to prove you are human. If you log in, this test goes away.




Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers