嵌套For循环变量批处理

时间:2015-08-21 20:18:20

标签: windows batch-file for-loop nested

这是我的代码

set list=test
del /Q failed_tests.txt
FOR %%a IN (%list%) DO (
    %%a > output_%%a.txt 

    FINDSTR /C:"[----------]" output_%%a.txt > check_run.txt

    for /f "delims=" %%x in (check_run.txt) do (
        set content=%%x
    )

    if not %content%=="[----------]" (
        echo Test Does Not Run >> failed_tests.txt
    ) else (
        echo Test Runs >> failed_tests.txt
    )
)
type failed_tests.txt

content似乎始终设置为"[----------]"。但是,如果output_test.txt / check_run.txt不包含"[----------]",则应将其设置为空字符串。

1 个答案:

答案 0 :(得分:1)

将@ JosefZ的建议纳入答案,以便显示答案。

SETLOCAL ENABLEDELAYEDEXPANSION
set list=test
del /Q failed_tests.txt
FOR %%a IN (%list%) DO (
    %%a > output_%%a.txt 

    FINDSTR /C:"[----------]" output_%%a.txt > check_run.txt

    set "content="
    for /f "delims=" %%x in (check_run.txt) do (
        set "content=%%x"
    )

    if not "!content!"=="[----------]" (
        echo Test Does Not Run >> failed_tests.txt
    ) else (
        echo Test Runs >> failed_tests.txt
    )
)
type failed_tests.txt

修改:根据评论保留警示性双引号。