'// get an array of the data file names
'// the function will return a boolean of FALSE if there
'// are no files that meet the file spec
Let vntLogDataFiles = fnFileSearch(strPath:=strPath, _
booSearchSubFolders:=False, _
strFileSpec:=strFileSpecLogData)
If TypeName(vntLogDataFiles) <> "Boolean" Then
ImportLogData wbLog, eLogType, vntLogDataFiles
booDirty = True
End If
' _____________________________________________________________________________
' FN FILE SEARCH
' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
' Descrip: Absolutely no one can explain how it is that the FileSearch function
' got deprecated out of Excel in XL2007; but it's out. So have to
' write a function that will take its place.
'
' This version is a synthesis of two versions I found in forums, one
' at Ozgrid written by Richard and the other on MrExcel, written by Nate.
'
' http://www.ozgrid.com/forum/showthread.php?t=71409&p=369573#post369573
' http://www.mrexcel.com/forum/showthread.php?p=1228168#1253882
'
' Args: strPath · · · · (string) The path to be searched
' booSearchSubFolders · · (boolean) Whether or not the function returns subfolders)
' strFileSpec · · · · · · (string) The file specification (filter)
'
' Returns: Variant · · · · · · · · (boolean) FALSE if there are no files that meet this
' file spec.
' (str arr) An array of filenames (or paths & filenames)
'
' Date Developer Comments
' ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
' 14 Apr 2011 G. Truby • initial version
' _____________________________________________________________________________
' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Public Function fnFileSearch(ByVal strPath As String, _
ByVal booSearchSubFolders As Boolean, _
Optional ByVal strFileSpec As String = "*.*") As Variant
' _____________________________________________________________________________
Const c_strThisProc As String = "fnFileSearch" '// used by error logging
Dim strErrMsg As String
On Error GoTo fnFileSearch_Error
'··········································································
Const c_lngMaxFiles As Long = 1048576 '// (current max # of rows in a worksheet)
Dim fsoFolder As Scripting.Folder, _
fsoSystemObject As Scripting.FileSystemObject, _
lngFileCount As Long, _
strFileName As String, _
strFileNames(1 To c_lngMaxFiles) As String, _
strReturnArr() As String, _
i&
Let strPath = fnAddPathSeparator(strPath)
Let strFileName = Dir(strPath & strFileSpec)
If Len(strFileName) = 0 Then
If Not booSearchSubFolders Then
Let fnFileSearch = False
Exit Function
End If
Else
Let lngFileCount = 1
Let strFileNames(lngFileCount) = strPath & strFileName
End If
Do While Len(strFileName) > 0 _
And lngFileCount <= c_lngMaxFiles
Let strFileName = Dir$
If Len(strFileName) > 0 Then
lIncrement lngFileCount
Let strFileNames(lngFileCount) = strPath & strFileName
End If
Loop
If booSearchSubFolders Then
Set fsoSystemObject = New Scripting.FileSystemObject
Set fsoFolder = fsoSystemObject.GetFolder(strPath)
FileSearchSubfolders fsoFolder, strFileNames, lngFileCount, strFileSpec
End If
If lngFileCount = 0 Then
Let fnFileSearch = False
Exit Function
End If
ReDim strReturnArr(LBound(strFileNames) To lngFileCount)
For i = LBound(strReturnArr) To lngFileCount
Let strReturnArr(i) = strFileNames(i)
Next i
Let fnFileSearch = strReturnArr
fnFileSearch_EarlyExit:
'¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
GoSub CleanUp
Exit Function
CleanUp:
'¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
Erase strFileNames
Set fsoSystemObject = Nothing
Set fsoFolder = Nothing
Return
fnFileSearch_Error:
'¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
If Not gc_booDebugMode Then GoSub CleanUp
'// the principle folder being checked is locked, we're dead-ended.
If Err.Number = 52 Then
Let fnFileSearch = False
Exit Function
End If
#If Not gccc_UseCentralErrorHandler Then
CentralErrorHandler strModule:=mc_strThisModule, _
strProc:=c_strThisProc, _
strExtraErrInfo:=strErrMsg ' , booEntryPoint:=True
#Else
MsgBox "Err #: " & Err.Number & vbCr _
& Err.Description, vbExclamation, "Error"
#End If
If gc_booDebugMode Then
Stop
Resume
End If
End Function '// fn File Search
' _____________________________________________________________________________
' FILE SEARCH SUBFOLDERS
' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
' Descrip: Setting this up as a separate sub so that it's easier to call it
' recursively. So, yes, it does get called resursively to run through
' each branch's subfolders.
'
' Args: fsoFolder · · · (scripting object) the folder to be searched
' strFileNames()· (string) the array of file names (including path) to
' be filled
' lngFileCount· · the current number of files found
' strFileSpec · · the [possibly] wild-card containing file specification
' being sought
'
' Returns: n/a, however strFileNames() & lngFileCount will be changed in the
' calling code since they are passed by reference.
'
' Date Developer Comments
' ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
' 25 May 2011 G. Truby • initial version
' _____________________________________________________________________________
' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Private Sub FileSearchSubfolders(ByRef fsoFolder As Scripting.Folder, _
ByRef strFileNames() As String, _
ByRef lngFileCount As Long, _
ByRef strFileSpec As String)
' _____________________________________________________________________________
Const c_strThisProc As String = "FileSearchSubfolders" '// used by error logging
Dim strErrMsg As String
On Error GoTo FileSearchSubfolders_Error
'··········································································
Dim fsoSubFolder As Scripting.Folder, _
strFileName As String, _
strPath As String
For Each fsoSubFolder In fsoFolder.SubFolders
Let strPath = fnAddPathSeparator(fsoSubFolder.Path)
Let strFileName = Dir$(strPath & strFileSpec)
Do While Len(strFileName) <> 0
lIncrement lngFileCount
Let strFileNames(lngFileCount) = strPath & strFileName
Let strFileName = Dir$
Loop
FileSearchSubfolders fsoSubFolder, strFileNames, lngFileCount, strFileSpec
EndOfLoop:
'¨¨¨¨¨¨¨¨¨
Next fsoSubFolder
Exit Sub
FileSearchSubfolders_Error:
'¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
If Err.Number = 52 Then
lIncrement lngFileCount
Let strFileNames(lngFileCount) = strPath & "« directory is locked »"
Resume EndOfLoop
End If
#If Not gccc_UseCentralErrorHandler Then
CentralErrorHandler strModule:=mc_strThisModule, _
strProc:=c_strThisProc, _
strExtraErrInfo:=strErrMsg ' , booEntryPoint:=True
#Else
MsgBox "Err #: " & Err.Number & vbCr _
& Err.Description, vbExclamation, "Error"
#End If
If gc_booDebugMode Then
Stop
Resume
End If
End Sub '// File Search SubFolders