batch file ping output to text file

放肆的年华 提交于 2019-12-13 06:13:57

问题


@echo off

ping www.google.com > pinglog.txt

pause

I have this command but my problem is when you add ">" or ">>" the command prompt will not show what is going on on the background. Is there a way that everything will be copied after doing the ping?

I am aware of this code as well:

@echo off

ping www.google.com > pinglog.txt
type pinglog.txt

pause

but still, the screen stays blank as if nothing happens. I hope someone can help


回答1:


Pipe to a Tee command.

ping www.google.com | cscript //nologo tee.vbs - t pinglog.txt

This is from filter.bat/vbs.

Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
If LCase(Arg(0)) = "tee" or LCase(Arg(0)) = "t" then Tee

Sub Tee
    On Error Resume Next
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set File = Fso.CreateTextFile(Arg(1), True)
    If err.number <> 0 then
        Outp.WriteLine "Error: " & err.number & " " & err.description & " from " & err.source
        err.clear
        wscript.exit
    End If
    Do Until Inp.AtEndOfStream
        Line=Inp.readline
        outp.writeline Line
        File.WriteLine Line
    Loop
End Sub

Tee

filter tee <Filename>
filter t <Filename>

Writes Stdin to a file and StdOut

Example

filter tee "%userprofile%\Desktop\winini.txt" < "%windir%\win.ini"


来源:https://stackoverflow.com/questions/28263236/batch-file-ping-output-to-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!