Parse time and runtime

I am trying to learn MS Batch, and I specifically tried to understand the aspects of "setlocal" and "enabledelayedexpression" when I came across vocabulary that I did not understand:

runtime and parsing time

0
terminology batch-file
source share
1 answer

The parser has different phases when parsing one line.
Thus, percentage expressions all expand when analyzing a line or block before a line (or any line in a block) is executed.

Thus, at runtime, they can no longer change.

set var=origin echo #1 %var% ( set var=new value echo #2 %var% ) echo #3 %var% 

He outputs

 #1 origin #2 origin #3 new value 

As during parsing # 2, it will be expanded to origin before any line of the block is executed. Thus, you can see the new value immediately after the block in # 3.

In contrast, slow expansion expands for each line just before line execution.

 setlocal EnableDelayedExpansion set var=origin echo #1 %var%, !var! ( set var=new value echo #2 %var%, !var! ) echo #3 %var%, !var! 

Exit

 #1 origin, origin #2 origin, new value #3 new value, new value 

Now in # 2 you see two different extensions for the same variable, since% var% expands when parsing the block, but !var! expands after the execution of the line set var=new value .

Learn more about the parser parser in SO: How are Windows command interpreter scripts (CMD.EXE) handled?

+4
source share

All Articles