[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] VBS如何规避因网络导致的延迟。

比如在一个局域网内有另外一台电脑:192.168.1.105
\\192.168.1.105\Music\黑豹乐队-靠近我.mp3
我要判断此文件是否存在,如果存在则a=1 不存在则a=0
'========正常思路是这样的=======
set fso =CreateObject("Scripting.FileSystemObject")
f1="\\192.168.1.105\Music\黑豹乐队-靠近我.mp3"
if fso.FileExists(f1) then a=1 else a=0
msgbox a
'================
正常情况下当然没问题
但是如果 192.168.1.105 这台电脑没有开机,用此命令的话会等很久才提示“0”
当然,上面的实例只是问题的具化,本质是网络是否通畅。

有没有办法规避这种情况?
VBS有没有可能给这个过程套个壳,限时(比如说500毫秒)判断是否成功,成功则继续;不成功则结束该过程并输出一个错误提示。  
或者有其他思路?


求姐,求带飞。

  1. Set WMI = GetObject("Winmgmts:")
  2. t1 = Time()
  3. Set objPing = WMI.Get("Win32_PingStatus.Address='192.168.1.105'")
  4. Msgbox DateDiff("s", t1, Time())
  5. Msgbox objPing.GetObjectText_()
复制代码

QQ 20147578

TOP

本帖最后由 yu2n 于 2015-4-27 16:54 编辑
  1. ' Ping 判断网络是否联通
  2. Function Ping(host)
  3.   If CreateObject("WScript.Shell").Run("cmd /c ping -n 1 -w 500 " & host, 0, True) = 0 Then
  4.     Ping = True
  5.   Else
  6.     Ping = False
  7.   End If
  8. End Function
复制代码
  1. ' Ping 判断网络是否联通
  2. Function Ping(host)
  3.   On Error Resume Next
  4.   Ping=False : If (host="") Then Exit Function
  5.   For Each o in GetObject("winmgmts:").ExecQuery _
  6.    ("select * from Win32_PingStatus where address='" & host & "' and timeout=500")
  7.     If (o.ResponseTime>=0) Then Ping=True : Exit For
  8.   Next
  9. End Function
复制代码
用法:
  1. If Ping("www.baidu.com") Then
  2. Msgbox "外网通。"
  3. Else
  4. Msgbox "外网不通。"
  5. End If
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

回复 3# yu2n


感谢    yu2n  的提示
代码已经用上:

Set ws = WScript.CreateObject("WScript.Shell")
TheIP = "192.168.1.105"
PingSet = " -n 1 -l 1 -w 500 "   
    TheState = ws.run("ping.exe " & TheIP & PingSet ,0, True)
    If TheState = 0 Then
        MsgBox "Ping = True"
    Else
        MsgBox "Ping = False"
    End If

判断网络是否畅通,当大于500毫秒即判断为超时。
久没上网,回复晚了。见谅见谅哈

TOP

回复 2# czjt1234


    代码我试过了,不是我想要的。应该是理解错我的出发点了。 还是感谢。

TOP

返回列表