|
- Function GetAllPath(Path$, Optional FileType$ = "*", _
- Optional FullName As Boolean = True, Optional IsFolder As Boolean = False)
- Dim dic As Object, i&, Fso As Object, Folder As Object
- Set dic = CreateObject("Scripting.Dictionary")
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set Folder = Fso.GetFolder(Path)
- i = 1
- Call GetPath(Folder, dic, FileType, IsFolder)
- If FullName Then GetAllPath = dic.keys Else: GetAllPath = dic.items
- Set Folder = Nothing: Set Fso = Nothing
- End Function
- Private Sub GetPath(ByVal Folder As Object, dic, Optional FileType$ = "*", Optional ByVal IsFolder As Boolean = False)
- Dim SubFolder As Object
- Dim File As Object, i&, arr
- If IsFolder Then
- For Each SubFolder In Folder.SubFolders
- If FileSerch(FileType, SubFolder.Name) Then dic.Add SubFolder.Path, SubFolder.Name
- Call GetPath(SubFolder, dic, FileType, IsFolder)
- Next
- Else
- For Each File In Folder.Files
- If FileSerch(FileType, File.Name) Then dic.Add File.Path & "" & File.Name, File.Name
- Next
- For Each SubFolder In Folder.SubFolders
- Call GetPath(SubFolder, dic, FileType, IsFolder)
- Next
- End If
- End Sub
- Private Function FileSerch(FileType$, fname$) As Boolean
- Dim arr, i&
- arr = Split(FileType, "|")
- For i = 0 To UBound(arr)
- If fname Like arr(i) Then FileSerch = True: Exit Function
- Next
- End Function
- Public Sub GetPathToRng(rng As Range, Path$, Optional FileType$ = "*", _
- Optional FullName As Boolean = True, Optional IsFolder As Boolean = False)
- Dim arr
- arr = GetAllPath(Path, FileType, FullName, IsFolder)
- rng.Resize(UBound(arr) + 1) = Application.Transpose(arr)
- End Sub
- Public Sub rngtest()
- [A3:E65536] = ""
- GetPathToRng [B3], ThisWorkbook.Path, , False, True
- a = [B65536].End(3).Row + 1
- GetPathToRng Range("B" & a), ThisWorkbook.Path, , False
- End Sub
复制代码 |
|