0

I am new to using enumerateFiles. I am trying to grab 50 of the most recent files in a directory of over 20,000. I cant seem to understand the syntax needed to get the OrderByDescending working. I've looked at the intellisense and I just don't understand it. Can someone assist ?

Dim root As String = "C:\Test"
Dim files = From file In 
Directory.EnumerateFiles(root).Take(50).OrderByDescending(Of String)
Gdog
  • 21
  • 1
  • 1
    Please research your question before asking https://stackoverflow.com/help/how-to-ask. There are plenty of examples of LINQ OrderBy https://stackoverflow.com/questions/7739705/sorting-files-from-directoryinfo-by-date-in-asp-net – Slai Mar 18 '18 at 12:36
  • Mind if i post another solution ? – Software Dev Mar 18 '18 at 16:23
  • @Slai - that link you posted is not for Directory.EnumerateFiles syntax , it uses getfiles. I did research this prior to posting. Perhaps its the same thing ? I dont know, hence my question. – Gdog Mar 18 '18 at 17:35
  • https://stackoverflow.com/questions/20486559/get-a-list-of-files-in-a-directory-in-descending-order-by-creation-date-using-c that will get you there. – Trevor Mar 19 '18 at 01:07
  • TY for the reply. I looked at that very example prior to posting. I dont really know C sharp so I hopefully converted it correctly to VB.net When I run the below code, it returns nothing. What am I doing wrong ? And what does "d" mean in this code ? I dont see where it is defined anywhere. Dim Directory2 As String = ("C:\Test") Dim di = New DirectoryInfo(Directory2) Dim filesOrdered = di.EnumerateDirectories().OrderByDescending(Function(d) d.CreationTime).[Select](Function(d) d.Name).ToList() – Gdog Mar 19 '18 at 13:13
  • sorry I know the code is not readable like that. I indented but its not showing as code for some reason. Still now working. I went in 4 spaces but not working again. :/ Dim Directory2 As String = ("C:\Test") Dim di = New DirectoryInfo(Directory2) Dim filesOrdered = di.EnumerateDirectories().OrderByDescending(Function(d) d.CreationTime).[Select](Function(d) d.Name).ToList() – Gdog Mar 19 '18 at 13:20

1 Answers1

0

There are different syntaxes for using LINQ.

The first one looks a little bit like SQL (From x In y) and I'm not familiar with it.

The other one looks like common functions on collections. These functions will be executed from left to right, so your not working example

Directory.EnumerateFiles(root).Take(50).OrderByDescending(Of String)

would first execute EnumerateFiles. Then it would take the first 50 items from the result of EnumerateFiles and then it would sort these first 50 items in descending order.

Since you want it the other way around (first sort descending by date, then take the first 50 items) you have to tweak your code a little bit.

Dim root As String = "C:\Test"
Dim files As IEnumerable(Of String) = IO.Directory.EnumerateFiles(root) _
                                                  .OrderByDescending(Of String)(Function(x As String) x) _
                                                  .Take(50)

Right now it would sort the files by name in descending order. If you like to sort by other criteria, you have to change the OrderByDescending part of the code, for instance like

....OrderByDescending(Of Date)(Function(x As String) IO.File.GetLastAccessTime(x))...

I hope this helps.

Nostromo
  • 1,177
  • 10
  • 28