Search code examples
filecmdstdoutflush

How to force Windows Comand Prompt to flush text output to a file line-by-line


I'm trying to log every ping in my Windows CE5.0 machine Command Prompt using

> ping 192.168.1.1 -t -l 60000 >> ping.txt  

The file starts with a single line of output and then only flushes after pressing 'ctrl+c'.

I was wondering if there was a way to force it to print in every new line.


Solution

  • I do not know any direct way to do this.

    But you could work around that by doing single ping requests in a (infinite) loop and write to the log file in the loop, like this:

    for /L %I in () do @(timeout 1 > nul & ping 192.168.1.1 -n 1 -l 60000 | find "TTL=" >> "ping.txt")
    

    The timeout 1 command establishes a one-second delay in every loop iteration in order to avoid heavy CPU load, > nul suppresses its console output.

    The find command is used to filter for lines containing TTL from positive replies (like Reply from 192.168.1.1: bytes=60000 time<1ms TTL=128). If you want, you can change that to findstr /B /C:"Reply from " /C:"Request " /C:"Ping request ", for example, to capture positive replies as well as negative ones like Request timed out. or Ping request could not find host ..., or you can remove it completely (also the |) to write the whole ping response to the file, including header and footer.