Using the file search object

G

Guest

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
 
J

Jim Cone

Chris,
There are enough complaints about FileSearch that I would not use it.
Here is another way...

Sub FindTheFiles()
'Jim Cone - San Francisco, USA - October 2006
Dim oFSO As Object
Dim oFile As Object
Dim oFolder As Object
Dim dteDate As Date
Dim strPath As String
Dim strName As String
Dim N As Long

strPath = "C:\ATI"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
For Each oFile In oFolder.Files
If oFile.DateLastModified > dteDate Then
dteDate = oFile.DateLastModified
strName = oFile.Name
End If
N = N + 1
Next 'oFile
Call FindTheSubFolderFiles(oFolder, N, dteDate, strName)
MsgBox N & " files" & vbCr & strName & " _ is latest file " _
& vbCr & "Dated _ " & dteDate

Set oFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing
End Sub

Function FindTheSubFolderFiles(ByRef oParentFolder As Object, _
ByRef lngR As Long, ByRef dteDte As Date, ByRef strNme As String)
Dim oSubFolder As Object
Dim oFile As Object
For Each oSubFolder In oParentFolder.SubFolders
For Each oFile In oSubFolder.Files
If oFile.DateLastModified > dteDte Then
dteDte = oFile.DateLastModified
strNme = oFile.Name
End If
lngR = lngR + 1
Next
FindTheSubFolderFiles oSubFolder, lngR, dteDte, strNme
Next 'oSubFolder
Set oSubFolder = Nothing
Set oFile = Nothing
End Function
-----------------

Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"ct60" <[email protected]>
wrote in message
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
 
G

Guest

Thanks Jim,

Very cool code. I am always partial to solutions that involve recursion.

Thanks and Best Regards,

Chris (ct60)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top