7

我正在使用 Microsoft Scripting Runtime (FSO) 来解析文件夹并生成其所有内容的列表,这些文件夹位于网络上,并且生成的路径最终超过 260。我拥有的最低代码如下:-

Private Sub ProcessFolder(ByVal StrFolder As String)
Dim Fl              As File
Dim Fldr            As Folder
Dim RootFldr        As Folder

Set RootFldr = FS.GetFolder(StrFolder)

    For Each Fl In RootFldr.Files
        Debug.Print Fl.Path
    Next

    For Each Fldr In RootFldr.SubFolders
        DoEvents
        ProcessFolder Fldr.Path
    Next

Set RootFldr = nothing    
End sub

在某个级别StrFolder长度变为 259 时,Set RootFldr ...文件夹行工作但For Each Fl In RootFldr.Files给出了错误76: Path not found,大概是因为内容导致路径超出 260 限制。

在 Windows 资源管理器中查看时,文件夹中有文件。当我将结果输出到工作簿时,我使用 Excel 作为此代码的主机。

为了对我的问题及其背景非常清楚,我需要使用 FSO(如果存在替代方案,很高兴看到替代方案)来访问其网络路径中深度超过 260 个字符的文件。我需要它作为 FSO,因为我拥有的工具是获取文件夹路径和文件路径、名称、创建和修改的大小。

4

3 回答 3

13

将 MAXFILE 阻碍的 DOS 路径名转换为本地 OS 路径名的技术已得到很好的建立和记录。总结:

  • 为使用驱动器号的路径添加前缀\\?\,例如\\?\C:\foo\bar\baz.txt
  • 为使用文件共享的路径添加前缀'\\?\UNC\,例如\\?\UNC\server\share\baz.txt.

FileSystemObject 也可以很好地工作,至少当我在 Windows 10 上测试您的代码时。在较旧的 Windows 版本或服务器上的网络重定向器中可能不一定是这种情况。通过使用 FAR 文件管理器创建具有长名称的子目录进行测试,并通过以下方式验证:

Dim path = "\\?\C:\temp\LongNameTest"
ProcessFolder path

出品:

\\?\c:\temp\LongNameTest\VeryLongFolderName0123456789012345678901234567890123456789012345678901234567890123456789\VeryLongFolderName0123456789012345678901234567890123456789012345678901234567890123456789\VeryLongFolderName0123456789012345678901234567890123456789012345678901234567890123456789\VeryLongFolderName0123456789012345678901234567890123456789012345678901234567890123456789\VeryLongFolderName0123456789012345678901234567890123456789012345678901234567890123456789\Chrysanthemum.jpg

长度为 488 个字符。要记住的事情:

  • 本机路径名必须是完整路径,不能是相对路径。换句话说,它们必须始终以驱动器号或共享名称开头,并从驱动器/共享的根目录开始。
  • 你得到了本地路径名,如果你显示它,不要忘记再次去掉前缀。
  • 未经测试但应该失败,文件名本身的长度(不包括目录名)仍有限制,不能超过 259 个字符。根本不应该是一个问题,因为用户也不能创建它们。
于 2016-07-23T11:44:49.413 回答
3

这需要一些创造性的编码,但ShortPath答案就是使用。

该工具用于创建根文件夹中每个文件夹和文件的列表,这些文件还显示其大小以及创建/修改日期。问题是当文件或文件夹的结果路径超过 260 时,Error 76: Path Not Found会引发错误并且代码不会捕获该区域的内容。

使用 Microsoft Scripting Runtime (FSO)ShortPath可以解决这个问题,但路径从人类可读到编码:-

完整路径 \\ServerName00000\Root_Root_contentmanagement\DPT\STANDARDS_GUIDELINES\VENDOR_CERTIFICATION_FILES\PDFX_CERTIFICATION_ALL\2006_2007\DPT\CompantName0\Approved\Quark\India under Colonial Rule_structure sample\058231738X\Douglas M. Peers_01_058231738X\SUPPORT\ADDITIONAL INFORMATION\IUC-XTG & XML file

短路径 \\lo3uppesaapp001\pesa_cmcoe_contentmanagement\CTS\S4SJ05~5\V275SE~8\PDM5D9~G\2N52EQ~5\HPE\GS9C6L~U\Approved\Quark\IQPSJ5~F\0CWHH1~G\DOFNHA~8\SUPPORT\A6NO7S~K\IUC-XTG & XML file

(请注意,我已经更改了保护 IP 和公司信息的完整路径,但大小相同)

您可以看到,虽然我可以将短路径传递给某人,并且他们可以将其放入 Windows 资源管理器以到达那里,但他们只需查看就知道它去了哪里,为了解决这个问题,使用了一个将文件夹路径保持为完整的字符串并遵循短路径正在做的事情。这个字符串就是我输出给用户的内容。下面的代码被删减了,但显示了我是如何实现它的。

简短的回答是ShortPathFSO 会解决这个问题,但路径不会很漂亮。

Dim FS              As New FileSystemObject
Dim LngRow          As Long
Dim StrFolderPath   As String
Dim WkBk            As Excel.Workbook
Dim WkSht           As Excel.Worksheet

Public Sub Run_Master()

Set WkBk = Application.Workbooks.Add

    WkBk.SaveAs ThisWorkbook.Path & "\Data.xlsx"

    Set WkSht = WkBk.Worksheets(1)

        WkSht.Range("A1") = "Path"
        WkSht.Range("B1") = "File Name"
        WkSht.Range("C1") = "Size (KB)"
        WkSht.Range("D1") = "Created"
        WkSht.Range("E1") = "Modified"

        LngRow = 2

        Run "\\ServerName00000\AREA_DEPT0_TASK000"

    Set WkSht = Nothing

    WkBk.Close 1
Set WkBk = Nothing

MsgBox "Done!"

End Sub

Private Sub Run(ByVal StrVolumeToCheck As String)
Dim Fldr            As Folder
Dim Fldr2           As Folder

Set Fldr = FS.GetFolder(StrVolumeToCheck)

    'This is the variable that follows the full path name
    StrFolderPath = Fldr.Path

    WkSht.Range("A" & LngRow) = StrFolderPath
    LngRow = LngRow +1

    For Each Fldr2 In Fldr.SubFolders
        If (Left(Fldr2.Name, 1) <> ".") And (UCase(Trim(Fldr2.Name)) <> "LOST+FOUND") Then
            ProcessFolder Fldr2.Path
        End If
    Next

Set Fldr = Nothing

End Sub

Private Sub ProcessFolder(ByVal StrFolder As String)
'This is the one that will will be called recursively to list all files and folders
Dim Fls             As Files
Dim Fl              As File
Dim Fldrs           As Folders
Dim Fldr            As Folder
Dim RootFldr        As Folder

Set RootFldr = FS.GetFolder(StrFolder)

    If (RootFldr.Name <> "lost+found") And (Left(RootFldr.Name, 1) <> ".") Then

        'Add to my full folder path
        StrFolderPath = StrFolderPath & "\" & RootFldr.Name

        WkSht.Range("A" & LngRow) = StrFolderPath
        WkSht.Range("D1") = RootFldr.DateCreated
        WkSht.Range("E1") = RootFldr.DateLastModified
        Lngrow = LngRow + 1

        'This uses the short path to get the files in FSO
        Set Fls = FS.GetFolder(RootFldr.ShortPath).Files

            For Each Fl In Fls
                'This output our string variable of the path (i.e. not the short path)
                WkSht.Range("A" & LngRow) = StrFolderPath
                WkSht.Range("B" & LngRow) = Fl.Name
                WkSht.Range("C" & LngRow) = Fl.Size /1024 '(bytes to kilobytes)
                WkSht.Range("D" & LngRow) = Fl.DateCreated
                WkSht.Range("E" & LngRow) = Fl.DateLastModified

                LngRow = LngRow + 1

            Next
        Set Fls = Nothing

        'This uses the short path to get the sub-folders in FSO
        Set Fldrs = FS.GetFolder(RootFldr.ShortPath).SubFolders
            For Each Fldr In Fldrs
                'Recurse this Proc
                ProcessFolder Fldr.Path
                DoEvents
            Next
        Set Fldrs = Nothing

        'Now we have processed this folder, trim the folder name off of the string
        StrFolderPath = Left(StrFolderPath, Len(StrFolderPath) - Len(RootFldr.Name)+1)

    End If
Set RootFldr = Nothing

End Sub

如前所述,这是对我有用的代码的删减版本,以举例说明用于超越此限制的方法。实际上,一旦我完成它,它似乎很简陋。

于 2016-07-25T10:55:04.590 回答
3

我曾经使用命令外壳的subst命令解决了这个问题。它允许您将驱动器号分配给本地路径(有点像网络共享)。

于 2016-07-26T18:45:12.197 回答