1

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.

4
  • 2
    1. Force set "content=" before for /f "delims=" %%x loop. 2.You need to enable Delayed Expansion and then use if not "!content!"=="[----------]" (note proper quoting). Force echo ON to see what happen... Commented Aug 21, 2015 at 20:31
  • That worked, thank you! Commented Aug 21, 2015 at 20:52
  • @JosefZ what if I want to add a for loop inside of the else statement that does something similar. Would I need to create another "content" like variable or and would I need to enable Delayed Expansion again? Commented Aug 21, 2015 at 22:45
  • 2
    @emilk No, you don't need to enable delayed expansion again. It is in effect until next ENDLOCAL statement. Add another variable if needed. Commented Aug 22, 2015 at 0:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

"no need for double quotes when clearing a variable (just set content= is fine)." - In theory. Unfortunately, some editors retain trailing spaces, so set content= may seem fine, but if there are trailing spaces on the line, those spaces will be assigned to the variable. Since they tend to invisibility when echoed, they can be hard to detect. hence the quoted version - which has become normal practice on SO - it guards agains superfluous trailing spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.