Question:
batch ping, with results to log file?
GadgetsPlus
2010-05-12 20:41:04 UTC
I'm fairly good with batch coding but I can't seem to get this simple script to work.
What am I missing??

My code:
ping www.yahoo.com >>C:\ping.txt

What is ends up in the log file:

C:\Documents and Settings\Joel\Desktop>ping www.yahoo.com 1>>C:\ping.txt
C:\Documents and Settings\Joel\Desktop>ping www.yahoo.com 1>>C:\ping.txt
C:\Documents and Settings\Joel\Desktop>ping www.yahoo.com 1>>C:\ping.txt
C:\Documents and Settings\Joel\Desktop>ping www.yahoo.com 1>>C:\ping.txt
hundreds of times...
Four answers:
anonymous
2010-05-12 21:18:23 UTC
I'll bet the problem is the NAME of your batch file... You didn't name it "ping.bat" perhaps? If you did, all you would accomplish is a looping file that does nothing but echo lines and call itself again in an infinite loop. Since there is no @ECHO OFF statement leading, each line echoes which you redirect into the output file. If you let it go, and did not manually BREAK the program to stop it, it would go forever until it ate up all of your disk space. Name your batch file "pingthem.bat" instead and you will find it will run correctly. Since ping is a program, you should use CALL to insure control returns to your batch file, however it is optional for a program, but mandatory .to daisy chain batch files. Here is a 3 line example that I have used on a LAN to test things, named "pingthem.bat" of course:



@echo off

for %%x in ( 0 1 2 3 4 5 6 7 8 9 ) do ping 192.168.0.%%x > pingthem.txt

for %%x in ( 10 11 12 13 14 15 ) do ping 192.168.0.%%x >> pingthem.txt



This sequence pings every IP address from 0 through 15 in the 192.168.0.XXX range and echoes the results into the "pingthem.txt" file.



I'll bet your naming of your batch file is your problem...



By the way, your line of :



ping www.yahoo.com >>C:\ping.txt



would be better expressed this way:



call ping.exe yahoo.com >> pingthem.txt



By using the optional EXE extension (IS it an EXE or a COM file? I forget) and the complete file name, you prevent any ambiguity when you have COM, EXE and BAT files of the same name. The problem is this: the command processor looks for COM, EXE and then BAT files in that order, true, but it searches the LOCAL directory for all 3 first before going through the entire PATH looking for the ambiguous file name. So, if there is a BAT file in the local directory, it will be found before the path is search for COM files, then EXE files, and finally BAT files. So, if you had a "ping.bat"

file in the current directory, even though it violates COM EXE BAT precedence, it is found and executed BEFORE the full path is searched for COM files and EXE files. Blame it all on Bill Gates. When he set precedence of COM, EXE and then BAT files, he should have done all of the path first for COM files, then EXE and finally BAT files. Why he made an exception for the current directory is beyond ME... Take a lesson here... DO NOT name batch files with actual program names. Always use an unambiguous name that can NOT be confused with any file name. One last thing, what version of windoze you are using determines whether you have a true command processor or an emulator, which ALSO can affect the precedence. As I recall, the last version with a true command processor was NT/2000. Anything newer is only a MSDOS emulator which has some very subtle differences which can cause unintended consequences. Not all MSDOS programs will run in an emulator, especially ones which deal with the hardware directly.
Nick C
2010-05-12 20:49:19 UTC
If your code is in a batch script, using echo off might fix that. If your are repeating your ping with a for loop or a goto statement, make sure you use >> instead of > (> will clear the file). Also, make sure there is no space between the www.yahoo.com and the >> as ping may parse the space as part of the argument.
anonymous
2010-05-12 20:45:20 UTC
Try ping.yahoo.com > C:\ping.txt



I get this ping.txt file:



Pinging yahoo.com [72.30.2.43] with 32 bytes of data:

Reply from 72.30.2.43: bytes=32 time=107ms TTL=51

Reply from 72.30.2.43: bytes=32 time=107ms TTL=51

Reply from 72.30.2.43: bytes=32 time=104ms TTL=51

Reply from 72.30.2.43: bytes=32 time=104ms TTL=51



Ping statistics for 72.30.2.43:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 104ms, Maximum = 107ms, Average = 105ms
anonymous
2016-02-28 08:33:36 UTC
First DOS command > batlog.txt Second DOS command >> batlog.txt and so on. The first redirection '>' creates the text file, and the two >> add later outputs.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...