Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, April 21, 2020 12:11 AM
Hi, please advise how to modify this code to get files, Order by Creation Time.
Latest should be received in Order. I tried below code
var files = Directory.EnumerateFiles(sPath, "*.pdf")
.OrderBy(f => f)
.Take(NumberOfFiles);
string[] xyz = files.ToArray();
Reason101
All replies (13)
Tuesday, April 21, 2020 12:42 AM | 1 vote
Use the following
https://stackoverflow.com/questions/4765789/getting-files-by-creation-date-in-net
DirectoryInfo info = new DirectoryInfo("PATH_TO_DIRECTORY_HERE");
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
foreach (FileInfo file in files)
{
// DO Something...
}
Adapt to *.pdf
var info = new DirectoryInfo("PATH_TO_DIRECTORY_HERE");
var files = info.GetFiles("*.pdf").OrderBy(p => p.CreationTime).ToArray();
Descending order
var info = new DirectoryInfo("PATH_TO_DIRECTORY_HERE");
var files = info.GetFiles("*.pdf").OrderByDescending(p => p.CreationTime).ToArray();
With Take
var info = new DirectoryInfo("PATH_TO_DIRECTORY_HERE");
var files = info.GetFiles("*.pdf").Take(takeValue).OrderByDescending(p => p.CreationTime).ToArray();
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
Tuesday, April 21, 2020 1:19 AM
OrderByDescending is taking files with larger size first.
var files = Directory.EnumerateFiles(sourcePath, "*.pdf").OrderByDescending(file => new FileInfo(file).CreationTime).Take(NumberOfFiles);
string[] xyz= files.ToArray();
But Iam looking for files with latest datetime stamp files first
Reason101
Tuesday, April 21, 2020 2:02 AM
OrderByDescending is taking files with larger size first.
var files = Directory.EnumerateFiles(sourcePath, "*.pdf").OrderByDescending(file => new FileInfo(file).CreationTime).Take(NumberOfFiles); string[] xyz= files.ToArray();
But Iam looking for files with latest datetime stamp files first
Reason101
First off where in your initial post is there mention of file size? Now with that said if I run the code I get the following from my document folder. Note the file size has zero to do with obtaining file with the latest date first,
DUA_ApplicationEnglish.pdf, 4/17/2020 5:19:48 AM, 170547
benefit2019_2020.pdf, 10/31/2019 8:06:17 AM, 476069
2019_mx5_om.pdf, 4/3/2019 6:21:27 AM, 61864699 largest file size
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
Tuesday, April 21, 2020 2:21 AM
I am not worried about file size, I want files given to xyz[] that have latest timestamp.
For Eg: there are 100 files in a folder, I want top 10 files to be passed to xyz[].
Those 100 files are from today, yesterday, 2 days before and so on...
I want xyz[] to have given only todays 10 files if available, Size doesnt matter.
But with above code, its just ignoring CreationTime and taking size of the file which I am not looking for.
Hope my requirement is clear
Reason101
Tuesday, April 21, 2020 5:20 AM
> But with the above code, its just ignoring CreationTime and taking
> size of the file...
It certainly does not. The code you posted, and Karen's code as well, work exactly as you asked. You are being fooled by something else. Are you doing additional processing after you get the list of file names?
Are you, perhaps, being fooled by the difference between CreationTime and LastWriteTime?
Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.
Tuesday, April 21, 2020 6:07 AM
Hi Reason101,
Thank you for posting here.
I tried the above codes, they are completely correct, I can't figure out why it is not useful to you, if possible, can you show us your results with the file name and CreationTime?
In addition, if you want to obtain the files created today, you can do so as follows.
var todayFiles = files.Where(x => x.CreationTime.Date == DateTime.Today.Date)
.OrderByDescending(x =>x.CreationTime)
.Take(10);
Best Regards,
Timon
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Tuesday, April 21, 2020 6:16 AM
Its not that I have to pick todays file, I need to pick the latest files.
Say if there were no files for today, then when my batch job run, it needs to pick the latest files available.
So how can I modify the code ?
Reason101
Tuesday, April 21, 2020 6:27 AM
Hi,
All I can provide is the same code as Karen.
In my opinion, those codes are correct. Can you show us the results?
Best Regards,
Timon
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Tuesday, April 21, 2020 6:35 AM
as per above logic, it is not changing the files are per latest creation date.
Please see screenshot below
Reason101
Tuesday, April 21, 2020 6:56 AM
Hi,
The time shown in the picture is the time when this file was last modified, which is Tim Roberts said: LastWriteTime, we can see the creation time in the file's properties.
The file order obtained using Karen's code may not be the same as shown in the picture, but that is the correct order.
Best Regards,
Timon
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Tuesday, April 21, 2020 2:17 PM
I am not worried about file size, I want files given to xyz[] that have latest timestamp.
For Eg: there are 100 files in a folder, I want top 10 files to be passed to xyz[].
Those 100 files are from today, yesterday, 2 days before and so on...
I want xyz[] to have given only todays 10 files if available, Size doesnt matter.
But with above code, its just ignoring CreationTime and taking size of the file which I am not looking for.
Hope my requirement is clear
Reason101
Okay I realize file size has nothing to do with this. Can you try the following? The idea is to keep the meat of the operation outside of where it's used.
Note the Between extension method also works not only on dates but numeric types also. And it can be done as an expression body
public static bool Between<T>(this T actual, T lower, T upper) where T : IComparable<T> =>
actual.CompareTo(lower) >= 0 && actual.CompareTo(upper) < 0;
Add the following class to your project.
public static class GenericExtensions
{
public static bool Between<T>(this T actual, T lower, T upper) where T : IComparable<T>
{
return actual.CompareTo(lower) >= 0 && actual.CompareTo(upper) < 0;
}
public static FileInfo[] GetNewestFiles(this DirectoryInfo sender, DateTime oldDate, string extension, int count)
{
var files = sender.GetFiles(extension)
.Where(item => item.CreationTime
.Between(oldDate, DateTime.Now))
.OrderByDescending(p => p.CreationTime.Date)
.Take(count)
.ToArray();
return files;
}
}
Use it as follows
var info = new DirectoryInfo("C:\\Users\\paynek\\Documents");
var files = info.GetNewestFiles(DateTime.Now.AddDays(-2), "*.pdf", 10);
foreach (var file in files)
{
Console.WriteLine($"{file.Name,100}, {file.CreationTime}");
}
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
Tuesday, April 21, 2020 2:21 PM
Yes, I'm thinking we need to use LastWriteTime for the purpose of the question.
For every expert, there is an equal and opposite expert. - Becker's Law
Tuesday, April 21, 2020 5:52 PM
var files = Directory.EnumerateFiles(sourcePath, "*.pdf").OrderByDescending(file => new FileInfo(file).CreationTime).Take(NumberOfFiles); string[] xyz= files.ToArray();
But Iam looking for files with latest datetime stamp files first
The original code posted by Karen will sort descending by the last date/time
*written* to with a small modification as noted by Tim:
DirectoryInfo info = new DirectoryInfo(sourcePath);
FileInfo[] files = info.GetFiles().OrderByDescending(p => p.LastWriteTime).ToArray();
foreach (FileInfo file in files)
{
// DO Something..
}
So will the code you posted with the same modification:
var files = Directory.EnumerateFiles(sourcePath, "*.pdf").OrderByDescending(file => new FileInfo(file).LastWriteTime).Take(NumberOfFiles);
string[] xyz = files.ToArray();
Note that the order may not match *exactly* what you will see in WIndows
Explorer, et al when multiple files have the *same* date/time stamp. In that
case the order of files with identical LastWriteTime values *may* appear in
a different order within that group.
- Wayne