Nested batch for loops

后端 未结 6 1596
面向向阳花
面向向阳花 2020-12-25 13:04

The following nested for-loop drives me mad (on Windows 7):

@echo off
SetLocal EnableDelayedExpansion

set TESTDIRS=fast mid slow
set TD=src\\test\\resources         


        
6条回答
  •  梦毁少年i
    2020-12-25 13:37

    It's not obvious! It's the special parsing of FOR!
    A FOR command is parsed directly after the escape/special character phase (for detecting the parenthesis), but as a result you can't using delayed or %%var expansion as parameters.

    FOR %%a in (%%%%B) do (
      FOR %%a in (1) DO ( <<< this %%a will not replaced with %%B
          echo %%a - shows 1, because %%a is the name of the inner variable
          echo %%B - doesn't work
      )
    )
    

    And also this can't work:

    set chars=abc
    FOR /F "delims=!chars!" %%N in (bla) DO ....  
    

    does not set a, b and c as delims, but !, c, h, a and r instead.

    EDIT: Within the parentheses the delayed expansion does work as expected however:

    set var=C:\temp
    For %%a in (!var!) DO echo %%a
    

    I would expect that you have to use a function to solve your problem.

提交回复
热议问题