Reputation: 105
Let's say I am looking for a file that has a name that starts with GLNO1_ I can have hundreds of files that start with those characters, but I want to retrieve the name of the file that starts with those characters that is the most recently modified.
For example, Lets say I have files GLNo1_1, GLNo1_2, GLNo1_3 etc. up to _1000 and number 556 is the file that was modified the most recent.
In VB.Net, how do I retrieve that file name.
The file extensions are of .csv
Upvotes: 1
Views: 4654
Reputation: 941317
You'll have to enumerate the files and pick the last one. That's a job for Linq:
Dim dir = New System.IO.DirectoryInfo("c:\foo\bar")
Dim file = dir.EnumerateFiles("GLNo1_*.csv").
OrderByDescending(Function(f) f.LastWriteTime).
FirstOrDefault()
If file IsNot Nothing Then
Dim path = file.FullName
'' etc..
End If
Never overlook the odds that there will be more than one "last one". If your program hasn't run for a while then more than one file could easily have been added by whatever software generates the *.csv files. You generally need to keep track of the files you've already seen before.
Upvotes: 5