C
ct60
Hello again,
Given a directory path, I would like to get the number of all files in that
directory and all its subdirectories excluding the all directory folders.
Plus, I would like to get the most recently updated file. The following code
seesm to work most of the time, but not always. Can anyone see a better way?
Note: I use a type to package the data.
Public Type DIR_FILE_DATA
numDocs As Long
lastModifedDate As Date
End Type
and now the code itself
Public Function get_FileData_Impl(dirPath As String) As DIR_FILE_DATA
Dim fs As FileSearch
Dim ret As DIR_FILE_DATA
Dim lastModifiedName As String
Set fs = Application.FileSearch
With fs
.LookIn = dirPath
.SearchSubFolders = True
' Note: this part generally excludes dir, but not always. I am also unclear
' what happens if this object encounters a zip file and how that is counted.
.Filename = "*.*"
' Sort by last modified, desc
.Execute msoSortByLastModified, msoSortOrderDescending
ret.numDocs = .FoundFiles.Count
If ret.numDocs > 0 Then
lastModifiedName = .FoundFiles(1)
ret.lastModifedDate = FileSystem.FileDateTime(lastModifiedName)
End If
End With
get_FileData_Impl = ret
End Function
Thanks in advance,
Chris
Given a directory path, I would like to get the number of all files in that
directory and all its subdirectories excluding the all directory folders.
Plus, I would like to get the most recently updated file. The following code
seesm to work most of the time, but not always. Can anyone see a better way?
Note: I use a type to package the data.
Public Type DIR_FILE_DATA
numDocs As Long
lastModifedDate As Date
End Type
and now the code itself
Public Function get_FileData_Impl(dirPath As String) As DIR_FILE_DATA
Dim fs As FileSearch
Dim ret As DIR_FILE_DATA
Dim lastModifiedName As String
Set fs = Application.FileSearch
With fs
.LookIn = dirPath
.SearchSubFolders = True
' Note: this part generally excludes dir, but not always. I am also unclear
' what happens if this object encounters a zip file and how that is counted.
.Filename = "*.*"
' Sort by last modified, desc
.Execute msoSortByLastModified, msoSortOrderDescending
ret.numDocs = .FoundFiles.Count
If ret.numDocs > 0 Then
lastModifiedName = .FoundFiles(1)
ret.lastModifedDate = FileSystem.FileDateTime(lastModifiedName)
End If
End With
get_FileData_Impl = ret
End Function
Thanks in advance,
Chris