Find most recent file date in folder

cjblake

New member
Joined
Sep 8, 2006
Messages
4
Programming Experience
Beginner
I need to check a directory that has thousands of files (> 40K and growing) for the most recent file date. I can't figure out how to do this without looping through every file. Is there any way to perform a sort prior to reading the files?

Thanks for any help!

CJ
 
Yes; scan the directory once, looking for the most recently dated file. Use a standard max algorithm:

Dim di as DirectoryInfo("C:\temp")
Dim fils() as FileInfo = di.GetFiles()
Dim maxDate as DateTime = 'minimum date possible
Foreach fi as FileInfo in fils
if maxDate < fi.WhateverDate then baxDate = fi.WhateverDate
next fi

'now we know the max ...



Then I'd use a filesystemwatcher component to watch for changes to files. Upon a change, update the maxDate.


Note that your solution makes no mention of keeping a list, only of knowing what was the most recently updated file
 
Thanks for your suggestion!

Are you suggesting that I keep the application running at all times? I was planning on having it just run once a day. The files won't actually get modified, so I'll be checking for new files being added. If new files aren't added daily, then there is something wrong with the program that's generating the files.

Thanks,
CJ
 
I forsee no problem with the program running constantly but if you only run it once a day, then it's acceptable to list the directory and calculate the newest file.

It might be easier to just have the program that is generating the file send an email when it's done generating. I've found in my work, that autonomy is good, and pandering to human laziness is the most effective way of maintaining a status; if youre going to rely on users to run this program once a day, who is to say they wont forget?

You could have the program run all the time, watch a selection of directories and email you when new files appear..
 
Thanks again. I've got no problem leaving it running, so I'll try your recommendation. I was planning on scheduling a task to run it daily, but this will work too.

Thanks for your help!
 
Hi, again.

I'm using the filesystemwatcher, but it doesn't seem to be picking up on the created file. Here's my code... What am I missing? The event never happens. I go in and create a txt file manually. I would expect that to trigger the event - shouldn't it? (Note that I'm very new and have never created an event like this). Thanks for the help once again!

Public Sub Main()
Dim strPath As String
maxDate = DateAdd(DateInterval.Year, -2, Now())
strEmail = GetEmailString()
strPath =
"c:\temp"



Dim watcher As New FileSystemWatcher()
watcher.Path = strPath
' Watch for changes in creation time only
watcher.NotifyFilter = (NotifyFilters.CreationTime)
' Only watch text files.
watcher.Filter = "*.txt"

' Add event handlers.
AddHandler watcher.Created, AddressOf OnCreated
' Begin watching.
watcher.EnableRaisingEvents = True

' Wait for the user to quit the program.
Console.WriteLine("Press 'q' to quit the Invoice NoFailure Notification.")
While Chr(Console.Read()) <> "q"c
End While
End Sub

' Define the event handler.
Private Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
' Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType)
Dim strNewFile As String = ""
strNewFile = e.FullPath
Dim fi As FileInfo = New FileInfo(strNewFile)
maxDate = fi.CreationTime
CreateFile(maxDate.ToString,
"c:\temp\test.txt")
End Sub

Sub CreateFile(ByVal strText As String, ByVal strPath As String)
If File.Exists(strPath) Then
File.Delete(strPath)
End If
'File.Create(strPath)
'File.AppendText(strText)
' Create the file.
Dim fs As FileStream = File.Create(strPath, 1024)
Dim info As Byte() = New UTF8Encoding(True).GetBytes(strText)
' Add some information to the file.
fs.Write(info, 0, info.Length)
fs.Close()

End Sub
 
Back
Top