0

Is there someone to explain exactly what is delayed environment variable expansion in simple terms or point to a post that can understand. Here is my questions

  • What can't I achieve without this
  • In which practical situations uses this
  • Any alternative
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • 2
    [ss64](http://ss64.com/nt/delayedexpansion.html) always is a good starting point. Also [stackoverflow](http://stackoverflow.com/search?q=[cmd]+delayed+expansion) has a lot of info. – Stephan Mar 03 '16 at 08:41
  • Take a look at this: http://stackoverflow.com/questions/29696734/how-to-put-variable-value-inside-another-variable-name-in-batch – MichaelS Mar 03 '16 at 09:29
  • I posted this explanation a long time ago. It explains it in a manner you will understand. http://ss64.org/viewtopic.php?id=985 – RGuggisberg Mar 03 '16 at 12:30
  • 1
    A very simple example [here](http://stackoverflow.com/questions/9405906/batch-file-add-remove-hosts-file-entries-syntax-error/9407130#9407130) – Aacini Mar 03 '16 at 15:18
  • 1
    Last but not least opening a command prompt window, entering `set /?` or `help set` and reading all output help text helps also to understand delayed variable expansion as explained on two small examples, an __IF__ and a __FOR__ example. – Mofi Mar 03 '16 at 17:39
  • Please also note that `DOS` is the OS from the 1980/1990s, and I don't think you are using that. –  Aug 17 '17 at 11:25

1 Answers1

1

%Var% is expanded when a line is read. As lines can have multiple commands, !var! is expanded when used. In MS-DOS !var! (accessed in script by %!var!%) is a legal variable name so you have to turn on a special mode to access. This is so MS-DOS batch files can run in CMD without editing.

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98