Enhancing CMD Scripting Capabilities: Exploring ‘setlocal enabledelayedexpansion’
Picture a scenario where variables in your CMD scripts could adapt on-the-fly, responding to changing conditions and inputs. This is the power that the “setlocal enabledelayedexpansion” command brings to the table. In this article, we will discover its ability to transform your static scripts into dynamic and adaptable tools.
In CMD, when you use a variable (e.g., %variable%) within a batch script, it gets replaced with its value when the command is parsed. This process is known as normal variable expansion. However, there are situations where you may need to expand a variable’s value at the time of execution rather than parsing. This is where Delayed Expansion comes into play. The “setlocal EnableDelayedExpansion” command enables Delayed Expansion mode in a batch script. By invoking this command, you can change the behavior of variables, allowing them to be expanded at the time of execution rather than parsing.
To see the “setlocal EnableDelayedExpansion” command in action and learn more about its practical applications, you can watch the following YouTube video:
Syntax and demonstration
Below we have run the same batch script one with normal “setlocal” and one with “setlocal EnableDelayedExpansion”.
@echo off SETLOCAL Set "_var=first" Set "_var=second" & Echo %_var%
As you can see in the above example the output of the code is “first”. The value of %_var% was read into memory BEFORE the Set command which changes it.
@echo off SETLOCAL EnableDelayedExpansion Set "_var=first" Set "_var=second" & Echo %_var% !_var!
In the case of the second example the output is “first second”. The value of the variable !_var! is evaluated as late as possible while the %_var% variable works just as before due to which !_var! gives us “second” as output and %_var% gives us “first”.
Delayed variable expansion is often useful when working with FOR Loops, normally an entire FOR loop is evaluated as a single command even if it spans multiple lines of a batch script. This is the default behavior of a FOR loop as shown below example:
@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%
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 will stubbornly display its initial value of 0 even as we set new values. 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%
Notice that within the for loop we use !variable! instead of %variable%.
As we wrap up our exploration of the “setlocal enabledelayedexpansion” command, it’s evident that this command is more than just a tool – it’s a mindset shift in the world of command line scripting. By enabling dynamic variable expansion, it empowers scriptwriters to create adaptable and responsive solutions. Embracing Delayed Expansion opens doors to new levels of efficiency and creativity in the CMD scripting realm. For more articles like this visit our site at Batch-man, you can also join our Discord and YouTube community.