Batch file to write ping results to a text file

135,019

Solution 1

If you want the timestamp in the file too, you'd need to put:

time /T >> filename.txt

In the first example you quoted:

@ECHO OFF
:LOOPSTART
time /T >> filename.txt
ping xxx.xxx.xxx.xxx -n 4 >> filename.txt
ping yyy.yyy.yyy.yyy -n 4 >> filename.txt
sleep -m 3000
GOTO LOOPSTART

You can play around with the following code to give you a more configurable timestamp:

set time_hh=%time:~0,2%
if %time_hh% lss 10 (set time_hh=0%time:~1,1%)
set time_mn=%time:~3,2%
set time_ss=%time:~6,2%
set time_ms=%time:~9,2%

echo %time_hh%:%time_mn%:%time_ss%.%time_ms% >> filename.txt

Put all of this in place of the time /t... line

Solution 2

To get the seconds (and milliseconds) replace time /T with echo %time% so you have:

@ECHO OFF
:LOOPSTART
echo %time% >> filename.txt
ping xxx.xxx.xxx.xxx -n 4 >> filename.txt
ping yyy.yyy.yyy.yyy -n 4 >> filename.txt
sleep -m 3000
GOTO LOOPSTART
Share:
135,019

Related videos on Youtube

SC.
Author by

SC.

My first computer was an 8-bit 48k RAM ZX Spectrum+. Ever since I've have trying to keep up with Technology as it quickly changes. Finance professional with some programming skills and an interest in Machine Learning.

Updated on September 18, 2022

Comments

  • SC.
    SC. over 1 year

    I know this is a variation of an already asked question but after research and several failed attempts I think I need some help.

    I would like to ping two websites repeatedly and record the time and results in a text file.

    I found this question Save Ping Output in a text file to be very helpful but the several versions I have tried do not work.

    If I execute the following in a command window, it creates the text file in my users directory as I would expect with the repeated pings recorded correctly.

    ping xxx.xxx.xxx.xxx -t > filename.txt
    

    But if I create the following ping.dat file and save it on my desktop. When I run it, it both opens a cmd window with the just time in it and also creates the desired textfile on my desktop. Unfortunately the file only contains the time and not the ping results and the results are obviously not 3seconds apart like expected.

    @ECHO OFF
    :LOOPSTART
    time /T
    ping xxx.xxx.xxx.xxx -n 4 >> filename.txt
    ping yyy.yyy.yyy.yyy -n 4 >> filename.txt
    sleep -m 3000
    GOTO LOOPSTART
    

    I assumed that my problem was related to how I was trying to write the results but if I modify the batch file, deleting the '>> filename.txt' reference all it does is open a cmd window which instantly fills up with time stamps.

    @ECHO OFF
    :LOOPSTART
    time /T
    ping xxx.xxx.xxx.xxx -n 4
    ping yyy.yyy.yyy.yyy -n 4
    sleep -m 3000
    GOTO LOOPSTART
    

    Hence my questions are

    A) Any idea what I am doing wrong?

    B) How do I change the time stamp so that it is HH:MM:SS rather than HH:MM

    All help appreciated.

    S.

    @LInker3000 Thanks for the reply. I should have mentioned this in my original question, but I have already tried that. When I added '>> filename.txt' to the timestamp, I now get a command window that fills up with "The process cannot acces the file because it is being used by another process.". Upon terminating the process, the created txt file only has one timestamp and the "Terminate Batch Job (Y/N)" text.

    • Naidim
      Naidim almost 13 years
      Linker3000's solution works just how you want, over here. I suspect that it is some anti-virus or syncing software which is locking the text file. Try turning it off and try it again.
    • SC.
      SC. almost 13 years
      It seems that my problem was naming the file ping.bat as soon as I changed that to pingbatch.bat it started working. The Sleep function isn't working though. Any suggestions on that?
    • Naidim
      Naidim almost 13 years
      I did not noticed it not working earlier, but maybe it wasn't. You could try timeout instead.
    • SC.
      SC. almost 13 years
      'timeout /T 5 /nobreak' worked great Thank you.
    • Linker3000
      Linker3000 almost 13 years
      There's also a crafty way to use PING for a delay (provided your computer is connected to a network): PING 1.1.1.1 -n 1 -w 3000 >NUL You just need to use an invalid address. Works great on older OSs that dont't have 'sleep'. Delay is in mS (3000 in the example)
    • SC.
      SC. almost 13 years
      Thanks Linker. Since everybody here seems to be so good at this let me ask one more question. Is there a way NOT to write the summary statistics to the text file. Ideally all I want is the 'Pinging xxx etc, Reply from...' or 'Pinging xxx etc, Request time out'
    • Naidim
      Naidim almost 13 years
      You could try ping xxx.xxx.xxx.xxx -n 4 | findstr Reply to get only the ping results.
    • SC.
      SC. almost 13 years
      I'm working on copying the ping results to a temporary file and then copying the first 2 lines (pinging & reply or request timed out) of that temp file into my main file. Just trying to learn how to use FOR /F to load the first two lines of the temp file.
    • SC.
      SC. almost 13 years
  • DavidPostill
    DavidPostill about 8 years
    Please read the question again carefully. Your answer does not answer the original question. OP wants to ping two different hosts.
  • NoobishPro
    NoobishPro over 7 years
    I just happened to stumble upon this and for me it's what I'm looking for... or it seems to be. Thing is; The script doesn't seem to do anything? It creates a log file and sets the host address in it, and then..... nothing?