115

I have a folder which contains many files. Is there any easy way to get the file names in the directory sorted by their creation date/time?

If I use Directory.GetFiles(), it returns the files sorted by their file name.

Senseful
  • 86,719
  • 67
  • 308
  • 465

7 Answers7

241

this could work for you.

using System.Linq;

DirectoryInfo info = new DirectoryInfo("PATH_TO_DIRECTORY_HERE");
FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
foreach (FileInfo file in files)
{
    // DO Something...
}
sharptooth
  • 167,383
  • 100
  • 513
  • 979
George Taskos
  • 8,324
  • 18
  • 82
  • 147
  • 'System.IO.FileInfo[]' does not contain a definition for 'OrderBy' – Umair Jabbar Mar 28 '11 at 13:15
  • 2
    It's an extension method... http://msdn.microsoft.com/en-us/library/bb383977.aspx – Ivo Dec 30 '11 at 19:03
  • 18
    I think there's no need to call the ToArray() method. – Ivo Dec 30 '11 at 19:03
  • AnyIdeas without using extension methods/LINQ because I'm targeting .NET 2.0 – Pratik Feb 27 '13 at 15:53
  • 14
    Consider using info.GetFiles().OrderBy(p => p.CreationTimeUtc) instead of CreationTime if there's any chance the files in the directory may have been created in different time zones (including daylight savings time) – Steve Feb 08 '17 at 15:26
59

You can use Linq

var files = Directory.GetFiles(@"C:\", "*").OrderByDescending(d => new FileInfo(d).CreationTime);
Sev
  • 15,401
  • 9
  • 56
  • 75
  • 2
    The DirectoryInfo solution is much faster then this (especially for network path) – jing Feb 05 '13 at 09:41
  • 1
    thank you @jing, for me the DirectoryInfo method takes about 0.5 seconds and this methods about 50 seconds and it's only 16000 files ^^ – Harry Jan 10 '23 at 08:45
9

If you don't want to use LINQ

// Get the files
DirectoryInfo info = new DirectoryInfo("path/to/files"));
FileInfo[] files = info.GetFiles();

// Sort by creation-time descending 
Array.Sort(files, delegate(FileInfo f1, FileInfo f2)
{
    return f2.CreationTime.CompareTo(f1.CreationTime);
});
Henrik
  • 111
  • 1
  • 7
6

This returns the last modified date and its age.

DateTime.Now.Subtract(System.IO.File.GetLastWriteTime(FilePathwithName).Date)
j0k
  • 22,600
  • 28
  • 79
  • 90
BMG
  • 489
  • 6
  • 9
2

If the performance is an issue, you can use this command in MS_DOS:

dir /OD >d:\dir.txt

This command generate a dir.txt file in **d:** root the have all files sorted by date. And then read the file from your code. Also, you add other filters by * and ?.

1

@jing: "The DirectoryInfo solution is much faster then this (especially for network path)"

I cant confirm this. It seems as if Directory.GetFiles triggers a filesystem or network cache. The first request takes a while, but the following requests are much faster, even if new files were added. In my test I did a Directory.getfiles and a info.GetFiles with the same patterns and both run equally

GetFiles  done 437834 in00:00:20.4812480
process files  done 437834 in00:00:00.9300573
GetFiles by Dirinfo(2)  done 437834 in00:00:20.7412646
Passer
  • 51
  • 5
0
            DirectoryInfo dirinfo = new DirectoryInfo(strMainPath);
            String[] exts = new string[] { "*.jpeg", "*.jpg", "*.gif", "*.tiff", "*.bmp","*.png", "*.JPEG", "*.JPG", "*.GIF", "*.TIFF", "*.BMP","*.PNG" };
            ArrayList files = new ArrayList();
            foreach (string ext in exts)
                files.AddRange(dirinfo.GetFiles(ext).OrderBy(x => x.CreationTime).ToArray());
Ata Hoseini
  • 117
  • 1
  • 4