VBScript 抛出“未设置对象变量"错误 [英] VBScript throws "object variable not set" error

查看:32
本文介绍了VBScript 抛出“未设置对象变量"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Set sh = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim counter, myNum, fileLine
myNum = 0
counter = 9000000
Do While myNum < counter
    myNum = myNum + 1
    Call GetConnections()
Loop

Function GetConnections()
    i = 0
    outFile = "netband_logger_vbs.tmp"
    Set objFile = objFSO.CreateTextFile(outFile, True)
    Set shExec = sh.Exec("netstat -e")
    Do While Not shExec.StdOut.AtEndOfStream
        fileLine = shExec.StdOut.ReadLine()
        objFile.Write fileLine & vbCrLf
        objFile.Close
    Loop
End Function

我有上面的 VBScript.我想要做的是运行 netstat -e 命令 9000000 次并将每一行输出逐行写入文本文件.并且每次在第一轮执行结束后,脚本都应该用新一轮执行的值覆盖 netband_logger_vbs.tmp 文件的先前内容.

I have the VBScript above. What I want to do is to run the netstat -e command 9000000 times and write every line of output to a text file line by line. And each time the after the first round of executions have terminated the script should overwrite the previous content of the netband_logger_vbs.tmp file with the values from the new round of executions.

目前我有两个问题:我似乎无法将整个输出写入我的 .tmp 文件,而且我还面临未设置对象变量"错误.

Currently I have two problems: I can't seem to write the entire output to my .tmp file and I am also faced with an "object variable not set" error.

推荐答案

您得到的错误可能是因为您在第一次迭代后关闭了文件句柄.要解决此问题,请在循环后移动 objFile.Close 行.

The error you're getting is probably because you're closing the file handle after the first iteration. To fix this move the line objFile.Close after the loop.

话虽如此,我还是不建议在这里使用 Exec 方法.在您的场景中,使用 CMD 和使用输出重定向要容易得多:

With that said, I wouldn't recommend using the Exec method here anyway. In your scenario it's much easier to shell out to CMD and use output redirection:

sh.Run "%COMSPEC% /c netstat -e >""" & outFile & """", 0, True

这篇关于VBScript 抛出“未设置对象变量"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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