Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function getFiles1(path As String, filter As String, subfolders As Boolean) As String()
- Set fs = Application.FileSearch
- Set rx = CreateObject("VBScript.RegExp")
- rx.Pattern = filter
- Dim files() As String
- Dim ub As Integer
- ub = -1
- Dim i As Integer
- With fs
- .LookIn = path
- .SearchSubFolders = subfolders
- .Filename = "*"
- If .Execute > 0 Then
- For i = 1 To .FoundFiles.Count
- Dim file As String
- file = .FoundFiles(i)
- If rx.test(file) Then
- ub = ub + 1
- ReDim Preserve files(ub)
- files(ub) = file
- End If
- Next
- End If
- End With
- Set fs = Nothing
- Set rx = Nothing
- getFiles = files
- End Function
- Function isEmptyArray(ByRef v() As String) As Boolean
- isEmptyArray = Len(Join(v)) < 1
- End Function
- Function getFiles2(path As String, filter As String, subfolders As Boolean) As String()
- Dim fso, rx
- Dim file As Variant
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set rx = CreateObject("VBScript.RegExp")
- rx.Pattern = filter
- Dim files() As String
- Dim ub As Integer
- ub = -1
- For Each file In fso.getFolder(path).files
- If rx.test(file.path) Then
- ub = ub + 1
- ReDim Preserve files(ub)
- files(ub) = file.path
- End If
- Next
- If subfolders Then
- For Each subfolder In fso.getFolder(path).subfolders
- Dim files_() As String
- files_ = getFiles2(subfolder.path, filter, True)
- If Not isEmptyArray(files_) Then
- For i = 0 To UBound(files_)
- ub = ub + 1
- ReDim Preserve files(ub)
- files(ub) = files_(i)
- Next
- End If
- Next
- End If
- Set fso = Nothing
- Set rx = Nothing
- getFiles2 = files
- End Function
- Sub test()
- Dim f1() As String
- Dim f2() As String
- f1 = getFiles1("D:\w", "xls$", True)
- f2 = getFiles2("D:\w", "xls$", True)
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement