醉里乾坤大,壶中日月长

tping(vbs)

上一篇 / 下一篇  2009-09-24 16:21:04

#给同事写的,放在这里记录下,简单的cmd中运行一次ping target -n 1来判断网络是否联通并且记录到指定文件中


'创建文件
Sub CreateTestFile(Byval sFilePath, Byval sFileContent)
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.CreateTextFile(sFilePath, true) '第二个参数表示目标文件存在时是否覆盖
    f.Write(sFileContent)
    'f.WriteLine(sFileContent)
    f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
    f.Close()
    Set f = nothing
    Set fso = Nothing
End Sub
'Example: CreateTestFile("C:\test.txt","Hello World")

'打开并写文件
Sub OpenAndWriteTestFile(Byval sFilePath,Byval sFileContent)
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(sFilePath, 8, false) '第二个参数 2 表示重写,如果是 8 表示追加
    f.Write(sFileContent)
    'f.WriteLine(sFileContent)
    f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
    f.Close()
    Set f = nothing
    Set fso = Nothing
End Sub
'Example: Call OpenAndWriteTestFile("C:\test.txt","Tell me the turth please!!!")

Function writeTofile(Byval sFilePath, Byval sFileContent) '将内容写到指定文件
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(sFilePath) Then
        OpenAndWriteTestFile sFilePath,sFileContent
    Else
        CreateTestFile sFilePath,sFileContent
    End If
     Set fso = Nothing
End Function
'writeTofile "c:\result.txt","heloo world"

Function tping(Byval sCommand)    '在cmd中运行sCommand并且回去stdout用来处理
    Set bjShell = CreateObject("WScript.Shell")
    Set re = New RegExp
    re.Pattern = "^Reply|^Request"
    cmdline = sCommand
    Set myping = objShell.Exec (cmdline)
    Do While Not myping.StdOut.AtEndOfStream
        strLine = myping.StdOut.ReadLine()
        result = re.Test(strLine)
        If result Then
            strText = Date&" "& Time & Chr(9) & strLine
        End If
    Loop
    tping = strText
End  Function

While True:  '永真循环
    writeTofile "c:\test.txt",tping("ping 10.255.250.232 -n 1")  '向指定文件"c:\test.txt"写结果
    WScript.Sleep 600000 '单位是毫秒,英文:ms,1000毫秒等于1秒。延时基本精确,非常精确是基本不可能的,总会有误差的,每个10分钟写一次
Wend


TAG:

 

评分:0

我来说两句

Open Toolbar