如何使用VBS将ping操作写入文本文件 [英] How to write a ping to a text file using VBS

查看:500
本文介绍了如何使用VBS将ping操作写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用VBS运行某些CMD命令,在此示例ping中,如何使用VBS而不是DOS将命令写入文本文件?

If I am using VBS to run some CMD commands, in this example ping, how could I write the command to a text file using VBS not DOS?

Set objCmdTest = WScript.CreateObject ("WScript.Shell")
Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\vbs\test.txt",8,true)

Output.WriteLine (objCmdTest.run ("ping failboat"))
Output.WriteLine (objCmdTest.run ("ping 8.8.8.8"))

所以这就是我正在使用的东西,但是会发生什么呢?脚本运行,文件制作完成,打开两个命令提示符以运行ping,最后文件中的文本显示为:

So this is what I'm working with however what happens is; The script runs, the file is made, 2 command prompts open to run the pings and finally the text inside the file reads:

0
0

当我更喜欢它具有ping输出时.

When I'd much prefer it to have the ping output.

仅供参考:请不要提出要求我使用DOS进行编写的建议,我想看看VBS如何完成我需要做的事情,有多种原因,谢谢!

FYI: Please don't offer suggestions that require me to use DOS for the writing, I'd like to see how VBS can do what I need for multiple reasons, thanks!

推荐答案

指令Output.WriteLine (objCmdTest.run ("ping failboat"))会将Run方法的返回值写入输出文件.如果要将命令输出附加到输出文件,则必须在命令中重定向输出:

The instruction Output.WriteLine (objCmdTest.run ("ping failboat")) will write the return value of the Run method to the output file. If you want to append the command output to an output file you have to either redirect the output in the command:

objCmdTest.run "%COMSPEC% /c ping failboat >>C:\vbs\test.txt", 0, True

或使用 Exec 代替Run:

Set ping = objCmdTest.Exec("ping failboat")
Do While ping.Status = 0
  WScript.Sleep 100
Loop
Output.WriteLine ping.StdOut.ReadAll

这篇关于如何使用VBS将ping操作写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆