October 21, 2013

Batch File: Append Ping Failures And Time Stamp To A Text File

So I’m trying to troubleshoot a problem where backups of a Windows 2008 server over a 1.5Mbps Wide Area Network (WAN) connection keep failing. Occasionally it will work, but most nights it will fail after running for several hours. We have a number of in-house network troubleshooting tools and I also know there are endless ping utilities that could assist. But instead of fancy utilities, I want a simple batch file to do the job. I want to know when this remote Windows server is unable to ping the destination network.


Create a simple batch file that outputs to a text file ONLY when a ping fails and also include the time of the failure.



If you run this ping command:

ping -n 1

The result will be this (assuming is inaccessible):

Pinging with 32 bytes of data:
Request timed out.

Ping statistics for
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),

Sell Art Online

So what I want to do is look for the “Lost =1” and when I find it, I want to append it to a text file along with a time stamp.

After concatenating many Google searches along with some trial and error, I’ve come up with a batch file that will ping a particular IP address once per second. If any individual ping results in a “Request timed out” the batch file will specifically look for “Lost = 1” in the ping results, and when found, will append the following string (with current date and time) to a new line in a text file:

Mon 10/21/2013 12:31:09.91 - [ Lost = 1 (100% loss)]

So every instance of the above line is a ping failure and all successful pings will NOT be recorded to the file. This allows me to open a small text file and at a glance see the specific times a particular IP address was inaccessible. The hope is to be able to line up ping failures with backup failures. But this could be used in troubleshooting many random timeout errors in applications, etc.



Below is the complete batch file (let’s call it pingloss.cmd) that will append failed pings to a text file along with a date and time stamp:

for /f "tokens=*" %%A in ('ping -n 1 %1 ^|find "Pinging %1"') Do echo %%A >>pingloss.txt


FOR /f "tokens=3 delims=," %%A IN ('ping -n 1 %1 ^|find "Lost = 1"') DO echo %date% %time% - [%%A] >>pingloss.txt

choice /c x /t 1 /d x >nul

goto START

– The for /f command scans the output of the ping command looking for specific area of text by delimiter (in this case a comma).
– The find command isolates the DO action of the for command to only act on occurrences of “Lost = 1”.
– The choice command is simply used to create a (1) second pause between pings.
– The purpose of the first line before the :START section is to append a single line to the text file that tells you which IP address it is pinging. This line denotes the beginning of a new test.

To use the batch file, simply run it like this (where x.x.x.x is the IP address you want to ping):

pingloss x.x.x.x

As soon as you run the file, the following line will be appended to a text file (in the same directory) called pingloss.txt:

Pinging x.x.x.x with 32 bytes of data:

No further lines will appear unless their is a ping failure.

So you can simply kick this batch file off in a command window and let it run while you do your testing. In this case, I’ll be kicking off the batch file and waiting for the nightly scheduled backup to occur. Next time this backup fails, I’ll check the pingloss.txt file to see if there is a ping failure that corresponds with the backup failure. At least then I can confirm (well, mostly) a network error and not a server error.

/ photo by Justin Gaynor

Tweet about this on TwitterShare on Google+Share on FacebookShare on RedditPin on PinterestShare on TumblrDigg thisShare on StumbleUponShare on LinkedInEmail this to someone

Please share your thoughts


  • jai

    What if I want run this batch program again a file that contains more than one ip.

    • You could specify multiple IP addresses on the command line and then repeat each line in the batch file that contains the “%1” variable and change it to “%2” and then “%3” and so on. Does that make sense? As for inputting from a text file, I’m a little rusty on how to do that, but I’m fairly certain it can be done. Here’s a possible place to start:


  • Steve Moreen

    David, thank you very for the script. It is almost exactly what I need. I would like to use it to check an IP microwave link which intersects the approach to an airport. I’ll set up two computers with one running the script and pinging each other. Would you be able to suggest a script modification which will not only append the file when the ping is lost, but also would append the file the first time the ping is found after a “lost” ping? This way I would know how long the outages are. I am a newbie to writing scripts and really appreciate any help you can give. Thanks!

  • Steve Moreen

    David, I made a few modifications on the script and it is going to work just as is. No reason for you to spend any time on it. Thanks again! Steve

  • Leigh Bennett

    Nice one!

    Testing 100Mb/s up and down fibre connection before it goes live.

  • romeo malancu

    hello, i’m a noob…What is described above seems what i want. But i can’t make it work. So, basicly, i’ve done the following: copy-psate the script in a txt file. Saved that txt file as pingloss.cmd. Where do i have to put the ping comand to my desired ip adress?

    • At a command prompt, type pingloss x.x.x.x (replace x’s with IP address).

      • romeo malancu

        hi, this is the result i’m getting when i give at command prompt the comand pingloss x.x.x.x (my ip instead of x’s of course). What have i done wrong?

        • Based on that screen capture, it looks like it should be creating the pingloss.txt and recording data. However, it isn’t recognizing “choice” as a valid command, which means it will loop incredibly fast (without the 1 second delay created by the choice command) and create a much bigger text file. Are you saying the text file isn’t being created? What version of Windows are you using? I’ve tested this on Windows 2008 R2 and Windows 7.