Say I have a directory structure like this:
TestFile1-2011140930.txt - last modified on 20/11/14 at 09:30
TestFile1-2011141301.txt - last modified on 20/11/14 at 13:01
TF2-2011140130.txt - last modified on 20/11/14 at 01:30
TF2-2011140630.txt - last modified on 20/11/14 at 06:30
The files names follow this name structure: {FileName}-{ddmmyytttt}.txt. I want to ensure that the most recently updated file by name is kept and the rest are deleted. For example, list entry 1 and list entry 2 have the same file name. List entry 2 was modified after list entry 2, so list entry 1 is deleted. Likewise list entry 3 and list entry 4 have the same filename. List entry 4 was modified after list entry 3 so list entry 3 was deleted.
I currently approach this by looping through every file in the directory but this is inefficient because I loop through all files - even those that were not created today and therefore do not have duplicates. I want to be able to do it with a Lambda expression. I have researched how to do this and I came across this question: How to find the most recent file in a directory using .NET, and without looping? and this code:
var myFile = directory.GetFiles()
.OrderByDescending(f => f.LastWriteTime)
.First();
I am new to Linq. I am proficient with SQL. Is it possible to modify the query above (like with SQL), so that the query meets my requirements or is my requirement too complex?
The code fragment above is C# but I am looking for a VB.NET solution.