【问题标题】:VBA - Identifying whether a string is a file, a folder or a web urlVBA - 识别字符串是文件、文件夹还是网页 url
【发布时间】:2012-04-01 06:13:01
【问题描述】:

我需要执行一些操作,通过传递一个字符串来启动,操作过程取决于字符串是文件、文件夹还是 web url。

仅供参考 - 对于文件,我将文件复制到存储库,对于文件夹,我正在创建快捷方式 .lnk 并将其复制到存储库,对于 Web url,我正在创建快捷方式 .url 并将其复制到存储库。

我开发了一个解决方案,但它不够强大;我因错误识别字符串而偶尔出现错误。我使用的方法是计算字符串中的点,并应用规则:

If Dots = 1 Then... it's a file.

If Dots < 1 Then... it's a folder.

If Dots > 1 Then... it's a website.

然后我使用我在网上找到的几个函数改进了这个:

Dots = Len(TargetPath) - Len(Replace(TargetPath, ".", ""))      ' Crude check for IsURL (by counting Dots)

If CheckFileExists(TargetPath) = True Then Dots = 1             ' Better check for IsFile

If CheckFolderExists(TargetPath) = True Then Dots = 0           ' Better check for IsFolder

问题是,我仍然有两种情况:

  1. 当文件名包含额外的点时,例如\Report.01.doc

  2. 当字符串是远程 Intranet 位置上的文件或文件夹时(我认为这可能被误识别为 Web url)。

任何正确方向的指针将不胜感激。

汤姆赫

【问题讨论】:

  • 感谢您的回复。 VBA 中是否提供正则表达式方法?这看起来好像它可以做我所追求的。
  • 是的,它们是CreateObject("vbscript.regexp") 或设置对 Windows 脚本宿主对象的引用。你会发现很多这类事情的正则表达式。您可能还想查看 FileSystemObject。它有很多不错的方法。

标签: string file url vba directory


【解决方案1】:

这可能会解决您的问题,或者至少会引导您解决一个问题:

Function CheckPath(path) As String
    Dim retval
    retval = "I"
    If (retval = "I") And FileExists(path) Then retval = "F"
    If (retval = "I") And FolderExists(path) Then retval = "D"
    If (retval = "I") And HttpExists(path) Then retval = "F"
    ' I => Invalid | F => File | D => Directory | U => Valid Url
    CheckPath = retval
End Function
Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
    'Purpose:   Return True if the file exists, even if it is hidden.
    'Arguments: strFile: File name to look for. Current directory searched if no path included.
    '           bFindFolders. If strFile is a folder, FileExists() returns False unless this argument is True.
    'Note:      Does not look inside subdirectories for the file.
    'Author:    Allen Browne. http://allenbrowne.com June, 2006.
    Dim lngAttributes As Long

    'Include read-only files, hidden files, system files.
    lngAttributes = (vbReadOnly Or vbHidden Or vbSystem)
    If bFindFolders Then
        lngAttributes = (lngAttributes Or vbDirectory) 'Include folders as well.
    Else
        'Strip any trailing slash, so Dir does not look inside the folder.
        Do While Right$(strFile, 1) = "\"
            strFile = Left$(strFile, Len(strFile) - 1)
        Loop
    End If
    'If Dir() returns something, the file exists.
    On Error Resume Next
    FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function
Function FolderExists(ByVal strPath As String) As Boolean
    On Error Resume Next
    FolderExists = ((GetAttr(strPath) And vbDirectory) = vbDirectory)
End Function
Function TrailingSlash(varIn As Variant) As String
    If Len(varIn) > 0 Then
        If Right(varIn, 1) = "\" Then
            TrailingSlash = varIn
        Else
            TrailingSlash = varIn & "\"
        End If
    End If
End Function
Function HttpExists(ByVal sURL As String) As Boolean
    Dim oXHTTP As Object
    Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
    If Not UCase(sURL) Like "HTTP:*" Then
    sURL = "http://" & sURL
    End If
    On Error GoTo haveError
    oXHTTP.Open "HEAD", sURL, False
    oXHTTP.send
    HttpExists = IIf(oXHTTP.Status = 200, True, False)
    Exit Function
haveError:
    Debug.Print Err.Description
    HttpExists = False
End Function

【讨论】:

  • 感谢您的回复。我已经在使用此代码的 Allen Browne 部分进行文件和文件夹检查。我有 2 个问题。 (a) 我假设这条线... If (retval = "I") And HttpExists(path) Then retval = "F" 应该是: If (retval = "I") And HttpExists(path) Then retval = "U " (b) 我认为 http 方法是在尝试 ping 页面。在这种情况下,对 https 和 ftp 有什么影响?还会产生真正的响应吗?
  • 是的,这是一个错字,应该是retval = "U"。对于您问题的另一部分,是的 HTTPS 和 FTP 生成相似(如果不相同)的状态代码:en.wikipedia.org/wiki/List_of_FTP_server_return_codes
  • 我现在有这个工作。我省略了“尾部斜杠”函数(实际上并未调用),并在 FileExists 函数的最后添加了 1 行... If Len(strFile)
  • @FrugalTPH 我一直在使用 MSXML2.XMLHTTP60 并取得了巨大的成功,而不是 CreateObject("MSXML2.XMLHTTP")
  • 我认为您应该按照作者的要求编写一个名为 isFolder 或 isFile 的函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
相关资源
最近更新 更多