-2

If have a directory with files that are created on different dates. I want to get the dates.

Is it possible to do this with a linq query, or must I first read all the files and use a foreach loop to get the dates.

Example:

List =

File_1  6/03/2016
File_2  6/03/2016
File_3  6/03/2016
File_4  6/03/2016
File_5  15/04/2016
File_6  21/04/2016
File_7  21/04/2016
File_8  21/04/2016

Result =

6/03/2016
15/04/2016
21/04/2016

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kris Martele
  • 29
  • 1
  • 7
  • 3
    It's not clear whether the dates are *in* the files, or just the file system entries. Have you tried anything yet? – Jon Skeet May 27 '16 at 08:47
  • You need to get the file properties, I show how to do that in the duplicate: `DateTime CreatedDate = Convert.ToDateTime(GetSpecificFileProperties(filePath, 4));` Another way you can do it using [FileInfo.GetCreationTime](https://msdn.microsoft.com/en-us/library/system.io.file.getcreationtime(v=vs.110).aspx) and another is the DOS Command `dir`. – Jeremy Thompson May 27 '16 at 08:51
  • There is no linq query in any of the answers to that duplicate question, and also the answer uses the shell which is complete overkill (and unnecessary) for the OP's question. – Matthew Watson May 27 '16 at 08:54
  • @KrisMatele I cannot answer this properly now because it was closed, however all you need if you want to use Linq to get a list of all the last modified times is this: `var dateInfo = Directory.EnumerateFiles(directoryName).Select(filename => new FileInfo(filename)).Select(info => new {info.Name, info.LastWriteTime});`. That will give you something you can use `foreach` on - each item in the list will have a `Name` and a `LastWriteTime` property. – Matthew Watson May 27 '16 at 08:56
  • @MatthewWatson I reopened, he wants the Create Date not last write – Jeremy Thompson May 27 '16 at 08:57
  • @MatthewWatson thanks for the info. I have the correct info for my foreach, but I was just wondering, if I could do it just with one linq statement. I will try a orderingby and groupby. – Kris Martele May 27 '16 at 09:09
  • @MatthewWatson I found an answer based on your info (see below). Thanks – Kris Martele May 27 '16 at 09:31

2 Answers2

0

Clearly you can't process the list of dates without iterating it, but you can use Linq to produce the sequence in the first place, like so:

var dateInfo =
    Directory.EnumerateFiles(directoryName)
    .Select(filename => new FileInfo(filename))
    .Select(info => new {info.Name, info.CreationTime});

That'll give you a list of FullName/CreationTime pairs, where FullName is the full path of the file, and CreationTime is the creation time of the file.

You can process it like so:

foreach (var item in dateInfo)
    Console.WriteLine($"{item.FullName} created on {item.CreationTime}");

If you just want the (unique) dates that the files were created on:

var uniqueDates = dateInfo.GroupBy(x => x.CreationTime.Date).Select(y => y.Key);

foreach (var date in uniqueDates)
    Console.WriteLine(date);

Finally, if you need the dates to be ordered:

var uniqueDates = 
    dateInfo.GroupBy(x => x.CreationTime.Date)
    .Select(y => y.Key)
    .OrderBy(z => z);

(And use .OrderByDescending() for the reverse order, of course.)

If you prefer Linq query syntax:

var uniqueDates =
    from    date in dateInfo
    group   date by date.CreationTime.Date into g
    orderby g.Key
    select  g.Key;

Or putting the entire thing in one Linq query (maybe getting a bit unreadble here, so you might want keep it as separate queries, but this is for completeness):

var uniqueDates =
    from date in
        from file in Directory.EnumerateFiles(directoryName)
        select new FileInfo(file).CreationTime
    group date by date.Date into g
    orderby g.Key
    select g.Key;
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

Based on the comment from @MatthewWatson.

I have make following linq statement

var dateInfo = Directory.EnumerateFiles(Dir).Select(filename => new FileInfo(filename)).Select(i => new { i.LastWriteTime }).GroupBy(g => g.LastWriteTime.Date);

That way I get all the different dates used in my directory.

Kris Martele
  • 29
  • 1
  • 7