setlocal ENABLEDELAYED EXPANSION

Asher00017
Asher00017 used Ask the Experts™
on
setlocal ENABLEDELAYED EXPANSION - what does this actually do?

Can anyone help me understand in detail?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
It is setlocal EnableDelayedExpansion (case insensitive, but only two words).
As the name tells, the setting enables a delayed expansion - of variables.
By default a batch file is executed line per line, and variables embedded in percent signs (like %ThisVar% are expanded (replaced by their content) immediately.
Now there are statement constructs which build "a single line" but contain multiple statements. FOR loops for example. Something like
setlocal EnableDelayedExpansion
for %%a in (1 2 3 4) do (
   set data=Data is %%a
   echo With percent: %data%
   echo delayed expansion: !data!
)

Open in new window

Delayed Expansion enables the second notation with exclamation marks.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Here are a couple of sites I have used in the past (and present for reference) to expand my knowledge of DOS BAT commands and techniques.  Bookmark these for reference, and also check out the links related to this question.

http://ss64.com/
http://ss64.com/nt/delayedexpansion.html
http://ss64.org/viewtopic.php?id=985

http://www.dostips.com/
http://www.dostips.com/DtTutoFramework.php

~bp
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
I've explained some of the context in my article http://www.experts-exchange.com/articles/673/Advanced-DOS-batch-pitfalls.html .
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
May I ask what led you to grading with "B" only?

Author

Commented:
I am very sorry. Must be a mistake since i was in a hurry! It was surely an "A".

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial