需要我创建的 Vb 代码的帮助

Need help on Vb code i have created

我正在做这个项目,我登录到路由器。 ping 某些 IP,如果我将数据包大小增加到 4,与需要时间的数据包计数 100 相比,O/p 将更快地显示百分比和往返的结果。所以需要代码,以便 Vb 像 "screening window" 一样实时检查文件,如果它在 cmd windows 上找到百分比和往返,它将发送 logout/exit/some 我的东西到 cmd并显示在 MsgBox .

我需要有关 Vb 实时检查 txt 文件的帮助,如果找不到,请关闭文件并再次打开以重新检查等等,或者可能像 "screening window" ..

求助.

我的原始代码如下。

====================

Private Sub Runcode()
i = Shell("\windows\system32\cmd.exe", vbNormalFocus)

Dim MY_SERVER_IP As String
Dim LocalHost As String
Dim SourceIP As String
Dim DestinationIP As String
Dim Number As String
Dim Size As String
MY_SERVER_IP = "97.1.2.2"


Application.Wait (Now + TimeValue("0:00:02"))


SendKeys "telnet " & MY_SERVER_IP & " 6000 -f D:\Report\PING.txt{ENTER}"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys " Command to Login the Router"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys " PING IP: SRN=0, SN=14, SIPADDR=some_Source_IP , DESTIP=Some_Destination_IP, CONTPING=NO, TIMES=4, PKTSIZE=1400; "

Application.Wait (Now + TimeValue("0:00:50"))
' for short packet Number also i have to wait around 50 sec
' here currently Times=4 means it will recieve 4 reply packets which will show the result in 4 sec
' but same if i change it to 50 it wll take around 30-50 sec
Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields As String
Dim buffer As String
buffer = "round-trip"
sFileName = "D:\Report\PING.txt"
''//Does the file exist?
If Len(Dir$(sFileName)) = 0 Then
MsgBox ("File not Created")
End If
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
Do While Not EOF(iFileNum)
Line Input #iFileNum, Fields
If Fields <> buffer Then
Call hint
Else
MsgBox ("didnt Work")
End If

Loop
Close iFileNum
End Sub
Close sFileName


Sub hint()

Dim myFile As String, text As String, textline As String, pkt As String, rndtrip As String

Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate "Telnet " & MY_SERVER_IP
objShell.SendKeys "Logout{enter}"
objShell.Application.Wait (Now + TimeValue("0:00:02"))
objShell.SendKeys "Exit{enter}"

myFile = "D:\Report\PING.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1


pkt = Trim(Mid(text, InStr(text, "Percent") + 0, (InStrRev(text, "packet") + 0) - (InStr(text, "Percent") + 1)))
rndtrip = Trim(Mid(text, InStr(text, "round-trip") + 0, (InStrRev(text, "ms") + 0) - (InStr(text, "round-trip") + 1)))

Call MsgBox("Percent : " & pkt & " Round-Trip : " & rndtrip & ".", vbOKOnly)

End Sub

尝试添加这一行。你的循环只会在文件存在时触发。

 If System.IO.File.Exists(myFile )

 Do Until EOF(1)
 Line Input #1, textline
 text = text & textline
 Loop
 Close #1
 else 

 ' if the file is not there rerun the method. 
 Runcode()

 end if