I've seen SETLOCAL ENABLEDELAYEDEXPANSION
& SETLOCAL DISABLEDELAYEDEXPANSION
in many batch files but what do the commands actually do?
When cmd.exe is started, delayed variable expansion is enabled or disabled by giving either the /v:on or /v:off option. In a batch file, delayed variable expansion can be enabled with setlocal enableDelayedExpansion .
Use setlocal to change environment variables when you run a batch file. Environment changes made after you run setlocal are local to the batch file. The Cmd.exe program restores previous settings when it encounters an endlocal command or reaches the end of the batch file.
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONSset variable modifications local to this script, i.e., the change to variable value disappears after the script ends. The variables revert to their original values. Without setlocal, the changes of variables preserves even after the bat script exits.
Setlocal syntaxBegins localization of environment changes in a batch file. Environment changes made after SETLOCAL is issued are local to the batch file. ENDLOCAL must be issued to restore the previous settings.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With