Batch how to set FINDSTR result to a variable and disable findstr print to console

The first problem is because you just take the first token from FOR. To solve it, you have two solutions, either to echo the complete line where the string is found...

for /f "tokens=*" %%i in ('FINDSTR /C:"Result Comparison Failure" %tmp_result_file%') do echo %%i

or echo the three tokens found

for /f %%i in ('FINDSTR /C:"Result Comparison Failure" %tmp_result_file%') do echo %%i %%j %%k

the second problem, the xx echoed two times is because you run two times the command. The first xx is the one from the first run, the second one is from the second run. If you want to prevent the second one, you need to use some additional logic. For example, setting a variable and then checking it. Warning, setting a variable in a loop requires enabling delayed expansion and using the !xx! syntax (see HELP SET for a detailed explanation)

setlocal enabledelayedexpansion
...
set result=
for /f "tokens=*" %%i in ('FINDSTR /C:"Result Comparison Failure" %tmp_result_file%') do (
  set result=%%i
)
if "!result!"=="" ( 
  echo !result!
) else (
  echo xx
)

Check out the FOR /F command.

for /f %i in ('FINDSTR  /C:"Result Comparison failure"  %tmp_result_file%') do ...

Tags:

Batch File