Example of delayed expansion in batch file

前端 未结 3 458
长发绾君心
长发绾君心 2020-11-21 14:40

Can someone give me an example of where a batch script would act differently with or without delayed expansion? Are there any situations where you would NOT want to use del

3条回答
  •  星月不相逢
    2020-11-21 15:18

    I wanted to add a great example on how "EnableDelayedExpansion" (EDE) can be useful outside of the ubiquitous FOR loop examples.

    Here is a line of earthquake data that I wish to parse (I call it it 1line.txt)

    ak_11574812 2015.04.29.193822 62.9525 -148.8849 1.0 9.5 1 49km S of Cantwell, Alaska

    The problem I ran into was that last segment of this line does not always start at the same column number. So I needed to create a flexible SET command that will accurately pluck out the last segment of this line.

    ECHO OFF
    setlocal enableDelayedExpansion
    set where=72
    set /p line=<1line.txt
    set locate=!line:~%where%,28!
    echo %locate%
    

    EDE allows me to place a variable (where) inside another variable (line). EDE will translate the variable bracketed by % first, then process the variable bracketed by ! and (in this case) push out the results into the "locate" variable.

提交回复
热议问题