Enable and Disable Delayed Expansion, what does it do?

setlocal enabledelayedexpansion means
delayed expansion definition
enabledelayedexpansion not working
batch variable expansion
setlocal powershell
batch enable expansion
delayed expansion ss64

I've seen SETLOCAL ENABLEDELAYEDEXPANSION & SETLOCAL DISABLEDELAYEDEXPANSION in many batch files but what do the commands actually do?

enabledelayeexpansion instructs cmd to recognise the syntax !var! which accesses the current value of var. disabledelayedexpansion turns this facility off, so !var! becomes simply that as a literal string.

Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

Using !var! in place of %var% accesses the changed value of var.

EnableDelayedExpansion - Windows CMD, EnableDelayedExpansion. Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL EnableDelayedExpansion command. By default expansion will happen just once, before each line is executed. The ! The !delayed! expansion is performed each time the line is executed, or for each loop in a FOR looping command. For simple commands this will make no noticable difference, but with loop commands like FOR, compound or bracketed expressions delayed expansion will allow you to always see the current value of the variable.

Copied from How do you use SETLOCAL in a batch file? (as dbenham indicated in his first comment).

Suppose this code:

If "%getOption%" equ  "yes" (
   set /P option=Enter option: 
   echo Option read: %option%

Previous code will NOT work becase %option% value is replaced just one time when the IF command is parsed (before it is executed). You need to "delay" variable value expansion until SET /P command had modified variable value:

setlocal EnableDelayedExpansion
If "%getOption%" equ  "yes" (
   set /P option=Enter option: 
   echo Option read: !option!

Check this:

set var=Before
set var=After & echo Normal: %var%  Delayed: !var!

The output is: Normal: Before Delayed: After

How do SETLOCAL and ENABLEDELAYEDEXPANSION work , files. If you need loop, then in most cases for has you covered. As the name suggests, delayed expansion makes batch processor delay expanding the variable to its value until it actually loops through it. But to make that happen, you need to do two things. 1. Enable delayed expansion. You can do this by doing setlocal ENABLEDELAYEDEXPANSION at the beginning of your script.

ALSO note that with SETLOCAL ENABLEDELAYEDEXPANSION you cant echo !!!!!! so:

echo my sentence! 123

will be outputed as:

my sentence 123

Setlocal - Local variables - Windows CMD, So yes, the value of %%i is corrupted because delayed expansion is enabled. CFG_%%A!") DO ( SETLOCAL disableDelayedExpansion SET "line=%%i"  How to Disable the Windows 10 Startup Delay To disable the Windows 10 startup delay, you just need to make a few edits in the Windows Registry. Standard Warning: Registry Editor is a powerful tool and misusing it can render your system unstable or even inoperable.

whats the difference between: %%a and %variable% variables , enabledelayedexpansion, Enables the delayed environment disabledelayedexpansion}, the ERRORLEVEL variable is set to 0 (zero). enable or disable delayed environment variable expansion. See SET /? for details. These modifications last until the matching ENDLOCAL command, regardless of their settings before the SETLOCAL command.

DelayedExpansion & Variable Replacement Issues, Furthermore, can be used to enable delayed expansion like this: "setlocal setlocal EnableDelayedExpansion set counter=1 for /f "tokens=*" %%i in (%2) do ( echo DISABLEEXTENSIONS enable or disable command processor extensions. Max's answer gives an example of where a batch script would act differently with or without delayed expansion. For the sake of completeness, let's answer another part of the question and show a situation where you would NOT want to use delayed expansion when your data contain an exclamation mark ! (and show two ways of processing such data):

setlocal, A classic batch script without Delayed Expansion will expand all variables character if delayed expansion is disabled, but once you enable it,  The only way to prevent this is by disabling Intel delayed launcher from msconfig. But here’s the bad side of it. The delay actually allows recovery to take place, the Intel ® Rapid recovery Technology restores the hard disk from the recovery partition. It is the windows default way

  • Possible duplicate of SETLOCAL and ENABLEDELAYEDEXPANSION usage question