【问题标题】:VBA: Deny long response by using UNC Path and FileSystemObjectVBA:使用 UNC 路径和 FileSystemObject 拒绝长响应
【发布时间】:2017-08-22 13:05:49
【问题描述】:

我尝试使用 UNC 路径和 FileSystemObject 获取网络中的目录,但如果网络目录不可用,则响应需要很长时间。我想这是因为扫描了很多网络,或者此时发送了更多的 ping。 那么有没有一种方法可以在使用 FSO 之前更快地检查网络目录的存在?

【问题讨论】:

  • 它是 VBA,但这没关系。

标签: vba unc filesystemobject


【解决方案1】:

我添加了文件检查(以及从子例程调用它的示例),然后是文件夹检查。两者的调用方式相同,都使用Dir 函数。我在文件检查中添加了一个错误处理程序,也值得为文件夹检查器添加它:

Sub Main()
    if FileExists("O:\Filenamehere") = TRUE then
        'do stuff
    end if
End Sub

'For a file
Function FileExists(ByVal FullPath As String) As Boolean
    On Error GoTo Hand
    If Dir(FullPath) <> "" Then
        FileExists = True
    Else
        FileExists = False
    End If
    Exit Function
Hand:
    Select Case Err.Number
        Case 52
            FileExists = False
    End Select
End Function

'For a directory
Function DirectoryExists(ByVal FullPath As String) As Boolean
    If Dir(FullPath, vbDirectory) <> "" Then DirectoryExists = True Else DirectoryExists = False
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2013-11-15
    相关资源
    最近更新 更多