I'm looking to fetch specific files from a directory based on their creation date. I want ".jpeg" files that their creation dates are more than 30 days before now to be fetched. how do we go about? I've written the code but I'm stuck in 'Where' clause
FileInfo[] fi;
DirectoryInfo di= new DirectoryInfo(@"C:\src_folder");
fi = di.GetFiles("*.jpeg").Where(....
Now here in 'Where' clause I'm not sure how do I go ahead for fetching files which are existed more than 30 days.
I think this can be helpful:
FileInfo[] fi;
DirectoryInfo di= new DirectoryInfo(@"C:\src_folder");
DateTime beginning = DateTime.UtcNow.AddDays(-30);
fi = di.GetFiles("*.jpeg")
.Where(file => file.CreationTimeUtc < beginning)
.ToArray();
GetFiles returns an array of FileInfo. Each of the elements will have CreationTimeUtc property you can use for to filter only items older than 30 days:
var limit = DateTime.UtcNow.AddDays(-30);
fi = di.GetFiles("*.jpeg").Where(f => f.CreationTimeUtc < limit).ToList();
It you want file that were modified earlier than 30 days ago use LastWriteTimeUtc instead of CreationTimeUtc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With