Hi All, this is my first post.
I'm really stuck on this one: I want my macro to find the biggest Excel file in a specified folder and its subfolders (say Music folder), then attach it to an email.
Found this beautiful code made by Mohammad Basem on this forum: it checks the folder and list all the files of a specified type.
Now what I need is to select only one file based on its size being the biggest/heaviest file in the folder.
Any idea?
I'm really stuck on this one: I want my macro to find the biggest Excel file in a specified folder and its subfolders (say Music folder), then attach it to an email.
Found this beautiful code made by Mohammad Basem on this forum: it checks the folder and list all the files of a specified type.
Now what I need is to select only one file based on its size being the biggest/heaviest file in the folder.
Any idea?
PHP:
Option Explicit'
' Information collected for each found file
'
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
'
' 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
'
' ———————————————————————————————————————————————————————————————————
'
Dim lCount As Long ' Counter for found files
Dim bSpec As Byte ' File spec counter
Dim blInclude As Boolean ' Include hidden folder or file
Dim blFound As Boolean ' Matching files found in current folder
Dim sFileSpec() As String ' File specs to find
' File system objects
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
' Clean-up
Set oParentFolder = Nothing
Set fsObj = Nothing
Exit Function
End If
sPath = IIf(Right(sPath, 1) = "\", sPath, sPath & "\")
' Check if any of the file specs exists in the target folder ‘sPath’
For bSpec = LBound(sFileSpec) To UBound(sFileSpec)
blFound = Dir(sPath & sFileSpec(bSpec), vbNormal + IIf(blIncludeHidden, vbHidden, 0)) <> ""
If blFound Then Exit For
Next bSpec
' Find files
If blFound Then ' Folder not empty and has at least one of the specified file specs
lCount = UBound(strFoundFiles) ' Initialise files counter
For Each oFile In oParentFolder.Files
blInclude = IIf(blIncludeHidden, True, (oFile.Attributes And vbHidden) = 0)
For bSpec = LBound(sFileSpec) To UBound(sFileSpec)
' Check if the file ‘oFile’ matches any of the specified specs
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 ' Get next file
End If
Next bSpec
Next oFile
End If
If blIncludeSubFolders Then
' Select next sub-folder
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
' Clean-up
Set oFile = Nothing ' Although it is Nothing
Set oFolder = Nothing ' Although it is Nothing
Set oParentFolder = Nothing
Set fsObj = Nothing
End Function