emilk
emilk

Reputation: 75

Nested For Loop Variables Batch

Here is my code

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 seems to always be set to "[----------]". However when output_test.txt/check_run.txt doesn't contain "[----------]" it should be set to an empty string.

Upvotes: 1

Views: 86

Answers (1)

WinTakeAll
WinTakeAll

Reputation: 258

Incorporating @JosefZ's suggestions into an answer so this shows as answered.

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

Edit: Retaining cautionary double-quotes as per comment.

Upvotes: 1

Related Questions