Enable and Disable Delayed Expansion, what does it do?

后端 未结 4 1982
既然无缘
既然无缘 2020-12-02 23:55

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

4条回答
  •  渐次进展
    2020-12-03 00:14

    Copied from How do you use SETLOCAL in a batch file? (as dbenham indicated in his first comment).

    Suppose this code:

    If "%getOption%" equ  "yes" (
       set /P option=Enter option: 
       echo Option read: %option%
    )
    

    Previous code will NOT work becase %option% value is replaced just one time when the IF command is parsed (before it is executed). You need to "delay" variable value expansion until SET /P command had modified variable value:

    setlocal EnableDelayedExpansion
    If "%getOption%" equ  "yes" (
       set /P option=Enter option: 
       echo Option read: !option!
    )
    

    Check this:

    set var=Before
    set var=After & echo Normal: %var%  Delayed: !var!
    

    The output is: Normal: Before Delayed: After

提交回复
热议问题