Advertisement
overloop

Application.filesearch Replacement For Office 2007

Jun 2nd, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. Function getFiles1(path As String, filter As String, subfolders As Boolean) As String()
  4.  
  5. Set fs = Application.FileSearch
  6. Set rx = CreateObject("VBScript.RegExp")
  7. rx.Pattern = filter
  8.  
  9. Dim files() As String
  10. Dim ub As Integer
  11. ub = -1
  12.  
  13. Dim i As Integer
  14.  
  15. With fs
  16.     .LookIn = path
  17.     .SearchSubFolders = subfolders
  18.     .Filename = "*"
  19.     If .Execute > 0 Then
  20.         For i = 1 To .FoundFiles.Count
  21.             Dim file As String
  22.             file = .FoundFiles(i)
  23.             If rx.test(file) Then
  24.                 ub = ub + 1
  25.                 ReDim Preserve files(ub)
  26.                 files(ub) = file
  27.             End If
  28.         Next
  29.     End If
  30. End With
  31.  
  32. Set fs = Nothing
  33. Set rx = Nothing
  34.  
  35. getFiles = files
  36.  
  37. End Function
  38.  
  39. Function isEmptyArray(ByRef v() As String) As Boolean
  40.     isEmptyArray = Len(Join(v)) < 1
  41. End Function
  42.  
  43. Function getFiles2(path As String, filter As String, subfolders As Boolean) As String()
  44.  
  45. Dim fso, rx
  46. Dim file As Variant
  47.  
  48. Set fso = CreateObject("Scripting.FileSystemObject")
  49. Set rx = CreateObject("VBScript.RegExp")
  50. rx.Pattern = filter
  51.  
  52. Dim files() As String
  53. Dim ub As Integer
  54. ub = -1
  55.  
  56. For Each file In fso.getFolder(path).files
  57.     If rx.test(file.path) Then
  58.         ub = ub + 1
  59.         ReDim Preserve files(ub)
  60.         files(ub) = file.path
  61.     End If
  62. Next
  63.  
  64. If subfolders Then
  65.     For Each subfolder In fso.getFolder(path).subfolders
  66.         Dim files_() As String
  67.         files_ = getFiles2(subfolder.path, filter, True)
  68.        
  69.         If Not isEmptyArray(files_) Then
  70.             For i = 0 To UBound(files_)
  71.                 ub = ub + 1
  72.                 ReDim Preserve files(ub)
  73.                 files(ub) = files_(i)
  74.             Next
  75.         End If
  76.        
  77.     Next
  78. End If
  79.  
  80. Set fso = Nothing
  81. Set rx = Nothing
  82.  
  83. getFiles2 = files
  84.  
  85. End Function
  86.  
  87.  
  88. Sub test()
  89.  
  90. Dim f1() As String
  91. Dim f2() As String
  92.  
  93. f1 = getFiles1("D:\w", "xls$", True)
  94. f2 = getFiles2("D:\w", "xls$", True)
  95.  
  96. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement