I just can’t imagine a function in the head that would bypass files comparing their names with DateTime.Now.AddDays(-1).ToShortDateString().ToString() And if it didn’t find it, then decrease the number in AddDays by another. There is a list of files that I extract from the folder. All files there have names in the form of dates (but this does not mean that they were loaded just then) 12/13/2018. * - for example. Imagine that now is the latest there 12.12.2018. How to extract the name of the latest file by comparing it with a mask. I understand that it should be brute force file comparison through forich, and if all the results of a fall are reduced by one more day and repeated.
- Why not sort the list of files by key-date? - MBo
- It will not work that way. files will be sorted into groups first dd, then mm, then YYYY. Example: 10/05/2018, 11/05/2018, 12/05/2018, 10/06/2018, etc. not the fact that the last in the list is the latest date. - Vlad Chekunaev
- I knowingly wrote about the sort key - I need to create the correct key from the name - for example, converting a string to a date, and not sorting it as a string - MBo
|
4 answers
I see the following algorithm:
- extract file names to list
- parse the date from the file name
- sort by date in reverse order
- select the first item
But try to write the code yourself)
UPD:
And, after all, how will such sorting look then?
var lastFileByName = Directory .GetFiles(@"C:\temp\", "*.zip") .Select(f => { DateTime date; var n = new FileInfo(f).Name.Replace(".zip", string.Empty); return DateTime.TryParseExact(n, "dMyyyy", null, DateTimeStyles.None, out date) ? new {Date=date, FileName = f} : null; }) .Where(it=> it != null) .OrderByDescending(it => it.Date) .FirstOrDefault()? .FileName; - It will not work that way. files will be sorted into groups first dd, then mm, then YYYY. Example: 10/05/2018, 11/05/2018, 12/05/2018, 10/06/2018, etc. not the fact that the last in the list is the latest date. - Vlad Chekunaev
- oneParse date means to select from text and translate it into DateTime format. - morphey83
- But I understood why parsing the date, and sorting the dates in any case will look like - compare the entire list with an example, if false decrease the example by 1 day, repeat. For this, the date is optional. - Vlad Chekunaev
- @VladChekunayev why compare the entire list with an example each time reducing the date, if the file with the largest date in the name is the desired file? And your version is also a worker - morphey83
- And, after all, how will such sorting look then?
DateTime[] dtArray;anddtArray.Max();? - Vlad Chekunaev
|
the simplest thing is this. You can also get the date of creating and changing the file
DateTime creation = File.GetCreationTime(@"file"); DateTime modification = File.GetLastWriteTime(@"file"); Well, then otsoritrvoat dates as you like
something like this
string [] fileEntries = Directory.GetFiles(targetDirectory); foreach (var d in fileEntries ) { dates.Add(File.GetLastWriteTime(d)); } var result = dates.Order(v => v).Last(); - there is a clarification from the author: "but this does not mean that they were loaded exactly then" - morphey83
- Well, I select by the modification date I think it will be more relevant - Sasuke
- The question was not about the date, but about the name of the files as a matter of fact - TEA
- yes, but as I understand the file name contains the date - Sasuke
- I do not consider this option, because Files were downloaded anyhow, several times overwritten. The freshest is now from 10/09/2018. It was just uploaded and edited. And the files are missing from 12/05/2018, so this option is not suitable. - Vlad Chekunayev
|
Apparently the problem is that you do not know how to parse the date.
string fileName = "5.9.2018"; DateTime date = DateTime.ParseExact(fileName, "dMyyyy", null); In general, it would be possible to ask about it.
- Honestly, I did not even know that it was possible to collect an array of dates in which there was a method to take the last date. - Vlad Chekunayev
|
Bottom line: did so, without parsing the date:
DirectoryInfo directory = new DirectoryInfo(@"C:\temp\"); var ffiles = directory.GetFiles(); string lastDate = ""; int t = 0; bool check = false; do { foreach (var filesdate in ffiles) { string inputDate = DateTime.Now.AddDays(t).ToShortDateString().ToString(); check = inputDate+".zip" == filesdate.Name; if (check) { lastDate = inputDate; break; } } t = t - 1; } while (check != true); Next, I already use lastDate
|