Quantcast
Channel: Group files based on Timestamp
Viewing all articles
Browse latest Browse all 7

Re: Group files based on Timestamp

$
0
0

urir10

My main question is not really how to parse the file name but how would i group them together. Lets say even the file name is only YYYYMMDD.txt nothing else.
Basically i have a method that zips the files. I want to be able to pass it a group of files based on their timestamp. So it will zip all file for Jan 1 together and all files for Jan 2 together etc.

You can try with the below code

//Change the file path here
                var directory = new DirectoryInfo("C:\\Test");
                //Query to group by records based on DatePart
                var myFile = (from f in directory.GetFiles()
                             .GroupBy(i => Regex.Replace(i.FullName, @"(.*?)(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})\.txt", "${year}/${month}/${day}"))
                              select f).ToList();
                //Read from grouping results
                foreach (var group in myFile)
                {
                    foreach (var file in group)
                    {
                        string filename = file.FullName;
                        //You can call the method here
                    }
                }


Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>