Can not understand what's『Function(d) New FileInfo(d).CreationTime』of this script mean

Hi there,

when I tried to find out how to read the newest file in the directory, I found out the script below.

Directory.GetFiles(“FOLDER PATH”).OrderByDescending( Function(d) New FileInfo(d).CreationTime ).First
this script come from here post 4==> How to select and load the newest file from a folder?

I understand almost 95% of it except I cant understand the function section where I Bolded that I’ve never seen that before

Could some help me to explain it or give me some direction where to google it.

Thank in advance

Have you googled Linq?

@tsunhsiao.hsu
Directory.GetFiles(“FOLDER PATH”) - is a string Array with the filepaths of the found files
.OrderByDescending( Function(d) New FileInfo(d).CreationTime )

  • OrderByDescending orders the files.
  • the so called lambda function specifies the criteria which is to used for the ordering
  • FileInfo is a class giving informations about files, we do create a new one for the particular file (keep in mind that this linq is looping over all items from Directory.GetFiles)
    FileInfo Class (System.IO) | Microsoft Learn
  • from the new created FileInfo we do use the property CreationTime for the Descending ordering
  • All items are descendend ordered on the creation time

.First() - taking the first element from ordered sequence

So the result is: taking the file with the newest Creation datetime

thanks now I know it relate to LINQ and Lambda
And I still have a question after reading LINQ and Lambda ,In 『Function(d) New FileInfo(d).CreationTime)」this script, I know alphabet “d” represents for argument, but what object would eventually pass in ? I cant tell

thanks !

thank you for your replying
now I know it relate to LINQ and Lambda
And I still have a question after reading LINQ and Lambda ,In 『Function(d) New FileInfo(d).CreationTime)」this script, I know alphabet “d” represents for argument, but what object would eventually pass in ? I cant tell

thanks !

1 Like

Using type inference, the compiler will determine that the type argument for d is a string (since GetFiles() returns a string array). You could rename the d to strFilename to make it easier to understand.

Directory.GetFiles(“FOLDER PATH”).OrderByDescending( Function(strFilename) New FileInfo(strFilename).CreationTime ).First

Thanks now I get it

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.