Enable and Disable Delayed Expansion, what does it do?

后端 未结 4 1981
既然无缘
既然无缘 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:27

    With delayed expansion you will able to access a command argument using FOR command tokens:

    setlocal enableDelayedExpansion
    set /a counter=0
    for /l %%x in (1, 1, 9) do (
     set /a counter=!counter!+1
     call echo %%!counter! 
    )
    endlocal
    

    Can be useful if you are going to parse the arguments with for loops

    It helps when accessing variable through variable:

     @Echo Off
     Setlocal EnableDelayedExpansion
     Set _server=frodo
     Set _var=_server
     Set _result=!%_var%!
     Echo %_result%
    

    And can be used when checking if a variable with special symbols is defined:

    setlocal enableDelayedExpansion
    set "dv==::"
    if defined !dv! ( 
       echo has NOT admin permissions
    ) else (
       echo has admin permissions
    )
    

提交回复
热议问题