How do I get the top two files from a folder

Hi,

Do can I get the top two files from a folder? The folder can have any number of files.

I have the below query to sort them in descending order. After I do that I need to pick the top two records.

Directory.GetFiles(in_PDFFilesOutputFolder,“*”).OrderByDescending(Function(d) New FileInfo(d).LastModifiedDate).ToArray

Hi @Krithi1 ,

Try below code Take(2) in the ending

Directory.GetFiles(in_PDFFilesOutputFolder,"*").OrderByDescending(Function(d) New FileInfo(d).LastModifiedDate).ToArray().Take(2)

Regards,
Arivu

Hi @arivu96

I see the below exception

image

I modified and trying this now- Directory.GetFiles(in_PDFFilesOutputFolder,“*”).OrderByDescending(Function(d) New FileInfo(d).LastModifiedDate).Take(2).ToArray

That’s because the code you typed in isn’t what arivu96 gave you. Look at it again.

Yes @postwick - that’s correct. Since that was not working for me, I modified the query as I pinged above and that worked

Thanks @arivu96 for the quick help. That helped me modify it a bit and try

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