Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable and Disable Delayed Expansion, what does it do?

Tags:

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

like image 839
Carl479 Avatar asked Mar 09 '14 04:03

Carl479


People also ask

How do I enable delayed variable expansion?

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 .

What does Setlocal command do?

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.

What does Enableextensions do?

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.

What is Setlocal and Endlocal in batch?

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.


1 Answers

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.

like image 86
Magoo Avatar answered Oct 03 '22 10:10

Magoo