Save Ping Output to Text File
Asked Answered
H

2

6

I am using this batch command to save ping output to a text file,

ping 10.226.2.10 -n 10 >>ping_ip.txt

But when i saved the above command in a batch file and trying to run it, in command prompt window my command gets converted to the below command...

ping 10.226.2.10 -n 10  1>>ping_ip.txt

you can see there is extra 1 in 1>> in the second command, i don't know how it came.. somebody please give your valuable opinion on the same

Henceforward answered 25/10, 2016 at 6:30 Comment(0)
C
5

This is just the normal behaviour.

In batch files you have some input/output streams:

  • 0 = standard input stream
  • 1 = standard output stream
  • 2 = standard error stream
  • 3-9 = user defined streams

Your >> operator implicitly redirects the standard output stream, that is, it is redirecting the stream number 1, and the cmd parser converts the command

command >> output

into

command 1>> output

showing the explicit command executed based in an implicit request

Creese answered 25/10, 2016 at 6:47 Comment(8)
ping 10.226.2.10 -n 10 >>ping_ip.txt through command prompt it is executing fine...Henceforward
Windows scripting behaviour little differ when you running commmands directly in console, and when you put your commands into a batch file and start whole batch from console.Pinnatifid
@NishantKumar, when you run the command from command line, you don't get echoed to console the command you are running, you directly see the output from the command, but in batch files, without echo off the parser shows the commands that will be executed before executing them.Creese
thanks for information, could you please tell me if i can resolve this and can make it run through batch file?Henceforward
@NishantKumar, there is nothing in your question pointing to any problem, >> and 1>> are just the same redirection. But if you prefer not to see it, put a @echo off as the first line in the batch file or use @ping 10.226.2.10 -n 10 >>ping_ip.txt to hide the console echoCreese
apologies, for not making it clear.. the problem here is when i am running the command "ping 10.226.2.10 -n 10 >>ping_ip.txt" through batch file it gets converted to what i stated in question... and when i checked the "ping_ip.txt" file, it contains "C:\Users\50012014.ACSIND\Desktop>ping 10.226.2.10 -n 10 1>>Ping.txt" copied over multiple times rather than the ping command outputHenceforward
@NishantKumar, it seems that your batch file is called ping.cmd or ping.bat, and when the parser sees ping, the batch file is called again, and again, and .... Rename the batch file (recommended) or change the line to ping.exe to avoid the recursive callCreese
Let us continue this discussion in chat.Henceforward
P
-1

Powershell with timestamp and appended to file.

ping.exe -t example.com | Foreach{"{0} - {1}" -f (Get-Date),$_} | out-file .\ping.txt -append
Purvis answered 28/5, 2021 at 7:47 Comment(1)
Could you please explain how this answers the question? I see no relation...Galleywest

© 2022 - 2024 — McMap. All rights reserved.