Nested batch for loops

后端 未结 6 1593
面向向阳花
面向向阳花 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条回答
  •  粉色の甜心
    2020-12-25 13:30

    Because nobody has mentioned it, here is the solution using batch subroutines and the CALL command.

    @echo off
    
    set TESTDIRS=fast mid slow
    set TD=src\test\resources\testsuite
    
    for %%d in (%TESTDIRS%) do call :process_testdir %%d
    goto :eof
    
    :process_testdir
    set CTD=%TD%\%1
    echo CTD: %CTD%
        REM Echos the expected path
    
    for /R %CTD% %%f in (*.fs) do (echo %%f)
        REM Echos as expected
    
    goto :eof
    

    I know GOTO is not very popular, but batch files were originally designed to use labels for control flow. The parenthetized control structure syntax was added later, and this question is an example of where it breaks down. The problem lends itself well to batch subroutines.

提交回复
热议问题