3

I am trying to filter the output of a running program (ping) and write the results to a file.

In the following example the ping program runs until CTRL-C is pressed:

ping www.google.com -t | findstr "Reply" >> file.txt

the result of this is the creation of 'file.txt' however, the contents are empty.

On the other hand, the following code, without piping to findstr works when interrupted with CTRL-C

ping www.google.com -t >> file.txt

What am I missing here?

jakob
  • 35
  • 1
  • 1
  • 5

4 Answers4

4

Control the spelling of the findstr command is case sensitive you can use /I for disabled case sensitive and dot not use double quote

Parameter t does not allow redirection in a file because the pipe receive the result before do FINDSTR if using the n parameter file with the number of Reply wait the end of replay for see result file.

ping www.google.com -n 200 | findstr /I reply  >> file.txt
Eric Siodmak
  • 151
  • 5
1

just try a for loop

@echo off
(for /l %%a in (0) do ping -t -n 1 www.google.com | findstr /i "reply")>file.txt
Endoro
  • 37,015
  • 8
  • 50
  • 63
0

the file would be filled, when the piped command finishes. But ping -t will never finish. You have to build your own loop (which has some advantages...):

:loop
<nul set /p "=%time% " >>file.txt
ping -n 1 www.google.de|find /i "Reply" >>file.txt
ping -n 2 -w 500 localhost >nul
goto :loop

the second ping is just a "500ms wait".

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

From documentation of ping:

-t: Ping the specified host until stopped.

Meaning it's waiting for user input to complete ping command.
The pipe on the other hand waited for input, and redirected whatever input was received to file.txt (in this case no input was received because ping is waiting for user input to complete ping command, so a blank file was created)

You can first redirect ping to file:
ping www.google.com -t >> file.txt

Then when you have enough pings (via Ctrl-C), filter with findstr:
findstr "Reply" file.txt >> file2.txt

Zimba
  • 2,854
  • 18
  • 26