簡體   English   中英

列出文件有效,但是如何列出文件夾(VBA,Excel)中的文件?

[英]listing files works, but how do I list files within folders (VBA, excel)?

我有一些在網上找到的代碼,效果很好。 它列出了指定文件夾中的所有文件及其屬性。 我只需要修改此函數即可返回文件列表。 我需要它查看所有文件的給定路徑。 因此,如果它是文件夾,請打開它並返回那些文件。 我最終需要用它來標記每個文件(因此在excel中的列中)。 如果有可能以某種方式創建層次結構,那將是個好主意...。但是,目前,只要找到它就打開一個文件夾並列出這些文件真是太棒了!

任何幫助都非常感謝!

Private Function fcnGetFileList(ByVal strPath As String, Optional strFilter As String) As Variant
'   Returns a one dimensional array with filenames
'   Otherwise returns False

Dim f As String
Dim i As Integer
Dim x As Integer
Dim FileList() As String

    If strFilter = "" Then strFilter = "*.*" 'can provide an extension to check?

    'set the path
    Select Case Right$(strPath, 1)
     Case "\", "/"
         strPath = Left$(strPath, Len(strPath) - 1)
    End Select

    ReDim Preserve FileList(0)

    f = Dir$(strPath & "\" & strFilter)

    Do While Len(f) > 0
        ReDim Preserve FileList(i) As String
        FileList(i) = f
        i = i + 1
        f = Dir$()
    Loop

    If FileList(0) <> Empty Then
        fcnGetFileList = FileList
    Else
        fcnGetFileList = False
    End If

End Function

想法:

添加Microsoft腳本運行時並使用文件系統對象(更好的文件/文件夾處理)

使此函數遞歸,將vbdirectory參數添加到dir函數,然后使用GetAtrr獲取有關每個文件的信息。 如果該文件再次是文件夾,請使用該新文件夾再次調用此函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM