[FONT=Consolas][SIZE=2][COLOR=Navy]Option Explicit
[COLOR=Green]'
' Information collected for each found file
'[/COLOR]
Public Type gstrFoundFileInfo
Path As String
Name As String
End Type
Function fnFindFiles(ByVal sPath As String, _
ByRef strFoundFiles() As gstrFoundFileInfo, _
ByRef lFilesFound As Long, _
Optional ByVal sPattern As String = "*.*", _
Optional ByVal blIncludeHidden As Boolean = True, _
Optional ByVal blIncludeSubFolders As Boolean = True) As Boolean
[COLOR=Green]'
' fnFindFiles
' ———————————
' Finds all files matching the specified file spec starting from the specified path and
' searches sub-folders if required.
'
' Reference required
' ——————————————————
' Microsoft Scripting Runtime
'
' Parameters
' ——————————
' sPath (String): Start-up folder, e.g. "C:\Users\Username\Documents"
'
' strFoundFiles (User-defined data type): A user-defined dynamic array to store the path
' and name of found files. The dimension of this array is (1 To nnn), where nnn is the
' number of found files. The elements of this array are:
' .Path (String) = File path
' .Name (String) = File name
'
' lFilesFound (Long): Number of found files.
'
' sPattern (String): Multi-spec of files to be returned. Separate the different specs by
' semicolon ‘;’ without spaces, e.g. "*.txt;abcdef?.doc?;jkl*.*"
' Optional parameter with default value = "*.*", that is all files.
'
' blIncludeHidden (Boolean): Specify whether to search and return hidden folders and files or not.
' Optional parameter with default value = True, which means hidden folders and files are returned.
'
' blIncludeSubFolders (Boolean): Specify whether to search sub-folders or not.
' Optional parameter with default value = True, which means sub-folders will be searched.
'
' Return values
' —————————————
' True: One or more files found and
' strFoundFiles = Array of paths and names of all found files
' lFilesFound = Number of found files
' False: No files found and
' lFilesFound = 0
'
' ———————————————————————————————————————————————————————————————————
' Using the function (sample code)
' ———————————————————————————————————————————————————————————————————
'
' Dim lFilesNum As Long
' Dim lCount As Long
' Dim strMyFiles() As gstrFoundFileInfo
' Dim blFilesFound As Boolean
'
' blFilesFound = fnFindFiles("C:\Users\MBA\Desktop", _
' strMyFiles, lFilesNum, "*.tx?;co*.xl*", False, True)
' If blFilesFound Then
' For lCount = 1 To lFilesNum
' With strMyFiles(iCount)
' MsgBox "Path:" & vbTab & .sPath & _
' vbNewLine & "Name:" & vbTab & .sName, _
' vbInformation, "Find Files"
' End With
' Next lCount
' Else
' MsgBox "No file(s) found matching the specified file specs.", _
' vbInformation, "Find Files"
' End If
'
' ———————————————————————————————————————————————————————————————————
'[/COLOR]
Dim lCount As Long [COLOR=Green]' Counter for found files[/COLOR]
Dim bSpec As Byte [COLOR=Green]' File spec counter[/COLOR]
Dim blInclude As Boolean [COLOR=Green]' Include hidden folder or file[/COLOR]
Dim blFound As Boolean [COLOR=Green]' Matching files found in current folder[/COLOR]
Dim sFileSpec() As String [COLOR=Green]' File specs to find[/COLOR]
[COLOR=Green] ' File system objects[/COLOR]
Dim fsObj As FileSystemObject
Dim oParentFolder As Object
Dim oFolder As Object
Dim oFile As Object
sFileSpec = Split(sPattern, ";")
Set fsObj = New FileSystemObject
On Error Resume Next
Set oParentFolder = fsObj.GetFolder(sPath)
If oParentFolder Is Nothing Then
On Error GoTo 0
fnFindFiles = False
[COLOR=Green] ' Clean-up[/COLOR]
Set oParentFolder = Nothing
Set fsObj = Nothing
Exit Function
End If
sPath = IIf(Right(sPath, 1) = "\", sPath, sPath & "\")
[COLOR=Green] ' Check if any of the file specs exists in the target folder ‘sPath’[/COLOR]
For bSpec = LBound(sFileSpec) To UBound(sFileSpec)
blFound = Dir(sPath & sFileSpec(bSpec), vbNormal + IIf(blIncludeHidden, vbHidden, 0)) <> ""
If blFound Then Exit For
Next bSpec
[COLOR=Green] ' Find files[/COLOR]
If blFound Then [COLOR=Green]' Folder not empty and has at least one of the specified file specs[/COLOR]
lCount = UBound(strFoundFiles) [COLOR=Green]' Initialise files counter[/COLOR]
For Each oFile In oParentFolder.Files
blInclude = IIf(blIncludeHidden, True, (oFile.Attributes And vbHidden) = 0)
For bSpec = LBound(sFileSpec) To UBound(sFileSpec)
[COLOR=Green] ' Check if the file ‘oFile’ matches any of the specified specs[/COLOR]
If LCase(oFile.Name) Like LCase(sFileSpec(bSpec)) And blInclude Then
lCount = lCount + 1
ReDim Preserve strFoundFiles(1 To lCount)
With strFoundFiles(lCount)
.Path = sPath
.Name = oFile.Name
End With
Exit For [COLOR=Green]' Get next file[/COLOR]
End If
Next bSpec
Next oFile
End If
If blIncludeSubFolders Then
[COLOR=Green] ' Select next sub-folder[/COLOR]
For Each oFolder In oParentFolder.SubFolders
blInclude = IIf(blIncludeHidden, True, (oFolder.Attributes And vbHidden) = 0)
If blInclude Then
fnFindFiles oFolder.Path, strFoundFiles, lFilesFound, sPattern, _
blIncludeHidden, blIncludeSubFolders
End If
Next
End If
fnFindFiles = UBound(strFoundFiles) > 0
lFilesFound = UBound(strFoundFiles)
On Error GoTo 0
[COLOR=Green] ' Clean-up[/COLOR]
Set oFile = Nothing [COLOR=Green]' Although it is Nothing[/COLOR]
Set oFolder = Nothing [COLOR=Green]' Although it is Nothing[/COLOR]
Set oParentFolder = Nothing
Set fsObj = Nothing
End Function[/COLOR][/SIZE][/FONT]