group by latest file
-
We have some list of files to copy from a ftp server. Sometimes the file in the source folder have files arrived on same date but @ different time and I would like to copy only latest file for each date. Ex: abc_sky_2014-01-15XYZ03-05-00.Dat abc_sky_2014-01-15XYZ03-08-00.Dat abc_sky_2014-01-16XYZ008-10-00.Dat abc_sky_2014-01-16XYZ008-13-00.Dat abc_sky_2014-01-16XYZ008-16-00.Dat From above files I want to fetch only the latest files for each date : abc_sky_2014-01-15XYZ03-08-00.Dat, abc_sky_2014-01-16XYZ008-16-00.Dat Can someone please help.
-
We have some list of files to copy from a ftp server. Sometimes the file in the source folder have files arrived on same date but @ different time and I would like to copy only latest file for each date. Ex: abc_sky_2014-01-15XYZ03-05-00.Dat abc_sky_2014-01-15XYZ03-08-00.Dat abc_sky_2014-01-16XYZ008-10-00.Dat abc_sky_2014-01-16XYZ008-13-00.Dat abc_sky_2014-01-16XYZ008-16-00.Dat From above files I want to fetch only the latest files for each date : abc_sky_2014-01-15XYZ03-08-00.Dat, abc_sky_2014-01-16XYZ008-16-00.Dat Can someone please help.
There is no "simple" built-in way to do that. You will have to read all the file names, and group them by name and date. You can then easily sort the times. I'd suggest using a regex to "split" the file name from the date and time, then using Linq to group the values.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
We have some list of files to copy from a ftp server. Sometimes the file in the source folder have files arrived on same date but @ different time and I would like to copy only latest file for each date. Ex: abc_sky_2014-01-15XYZ03-05-00.Dat abc_sky_2014-01-15XYZ03-08-00.Dat abc_sky_2014-01-16XYZ008-10-00.Dat abc_sky_2014-01-16XYZ008-13-00.Dat abc_sky_2014-01-16XYZ008-16-00.Dat From above files I want to fetch only the latest files for each date : abc_sky_2014-01-15XYZ03-08-00.Dat, abc_sky_2014-01-16XYZ008-16-00.Dat Can someone please help.
var directory = new DirectoryInfo(@"F:\Root\");
var files = from f in directory.EnumerateFiles("*.dat")
group f by f.Name.Substring(0, 21) into g
select g.Last();foreach (var file in files)
{
Console.WriteLine(file.Name);
}