6

Hi I have many files in a folder. Those files have date and time in the file name in specific format. I need to extract the date from the name and then sort it by date in ascending order. File Name example :-

format_type_2011-07-12-13-00-12.txt

Earlier I was using by createTime. But now requirement is changed.

var Files = new DirectoryInfo(FileDirectory).GetFiles()
                                                            .OrderBy(f => f.CreationTime)
                                                            .ToList();

How do i do it? Any help is appreciated.

4
  • write a function to extract the date and replace the question marks with a call to that function....? Commented Jul 8, 2011 at 15:53
  • 1
    Similar to this question: stackoverflow.com/questions/1199006/… Commented Jul 8, 2011 at 15:54
  • Are the files accessed or updated after the initial creation? Is there a reason you cannot use the FileInfo.CreationTime (or LastWriteTime) property? Commented Jul 8, 2011 at 15:55
  • No Can't use FileInfo.CreationTime anymore. Coz sometimes Files are uploaded in one shot using FTP...that keeps the creationTime same for all the files.. Commented Jul 8, 2011 at 15:57

6 Answers 6

7

This should work:

var di = new DirectoryInfo(FileDirectory);
var Files = di.GetFiles()
              .OrderBy( f => f.Name.Substring(f.Name.LastIndexOf('_')+1)
              .ToList();

Since your file names (minus the format info) are already in ISO8601 order (year first, then month, then date, etc.) you can just sort based on the string w/o having to convert to a date.

Sign up to request clarification or add additional context in comments.

1 Comment

Seems like should but it doesn't. "At least one object must implement IComparable".
2

You can use ordinary string operators in your orderby statement to exract the part you want to sort on:

    string f1 = "Foo_2011-07-12-13-00-12.txt";
    string f2 = "Bar_2011-07-12-13-00-15.txt";
    string f3 = "Blah_2011-07-12-13-00-11.txt";

    int sortRelevant = "0000-00-00-00-00-00.txt".Length;

    List<string> files = new List<string>() { f1, f2, f3 };

    var sorted = (from f in files orderby f.Substring(f.Length - sortRelevant) select f);

    foreach (string fs in sorted)
    {
        Console.WriteLine(fs);
    }

Comments

0

I think LastWriteTime is what you are looking for. Here is the MSDN link: http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.lastwritetime.aspx

also the link to FileInfo if needed: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

1 Comment

No LastWriteTime is not I am looking for. I need to sort files by date and time and this date comes from the File Name itself.
0

You are close:

var Files = new DirectoryInfo(@"C:\").GetFiles()
                                     .OrderBy(f => f.LastWriteTime)
                                     .ToArray(); //or .ToList() whatever suits you best

1 Comment

@Adrain. I need to sort it by date and time from file name. I can't use FileInfo.
0

I think that this code should work for you:

var Files = new DirectoryInfo(@"W:\").GetFiles().OrderBy(f=> f.LastWriteTime).ToList();

Comments

0
var Files = new DirectoryInfo(FileDirectory)
                   .GetFiles()
                   .OrderBy(f => f.Name.Substring(f.Name.Length - 23, 19)
                   .ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.