Searching for files DateModified value
-
Hi, I want to write a function that will search any given folder and all its' subfolders and return any files that have a date modified later than a certain date. Looking at System.IO Namespace, but can't find anything. Any ideas? Graham
look for the fSO object Dim fso As New FileSystemObject, fil As File Set fil = fso.GetFile("c:\detlog.txt") ' Get a File object to query. Debug.Print "File last modified: "; fil.DateLastModified ' Print info
-
look for the fSO object Dim fso As New FileSystemObject, fil As File Set fil = fso.GetFile("c:\detlog.txt") ' Get a File object to query. Debug.Print "File last modified: "; fil.DateLastModified ' Print info
-
Hi, I want to write a function that will search any given folder and all its' subfolders and return any files that have a date modified later than a certain date. Looking at System.IO Namespace, but can't find anything. Any ideas? Graham
Both the
FileInfo
andDirectoryInfo
classes inherit fromFileSystemInfo
, which has the properties you need. For example,LastWriteTime
is the modified date/time. Just off the top of my head:Imports System.IO
Imports System.Collections...
Public Shared Function Search(ByVal folder As String, ByVal minDate As Date) As FileInfo()
Dim files As New ArrayList()
Dim folderEntry As New DirectoryInfo(folder)
SearchInternal(folderEntry, files, minDate)
Return DirectCast(files.ToArray(GetType(FileInfo)), FileInfo())
End FunctionPrivate Shared Sub SearchInternal(ByVal folder As DirectoryInfo, ByVal files As ArrayList, ByVal minDate As Date)
If folder.Exists Then
' List the files in the current directory
Dim file As FileInfo
For Each file In folder.GetFiles()
If file.LastWriteTime > minDate Then
files.Add(file)
End If
Next' Search the subdirectories Dim subfolder As DirectoryInfo For Each subfolder In folder.GetDirectories() SearchInternal(subfolder, files, minDate) Next End If
End Sub
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer