Get the latest file with a specific name from a folder

hello. I’m trying to get the most recent folder of files containing a specific word from the folder at path A.

AAA.txt (05/20)
BBB.txt (05/21)
a12.txt(05/22)
ABA.txt (05/23)
AAA.txt (05/24)
B56.txt (05/25)

If such a file exists, it tries to import the file created on the 24th, which is the most recent file among the files with the name AAA.

I tried using OrderByDescending(Function(d) New FileInfo(d).CreationTime), but it just fetches the most recent files from that folder, not files with specific words.

How can I get the most recent file with a specific name?

@dlqhqo98

Try below expression.

             Directory.GetFiles(str_folder,"*AAA*").OrderByDescending(Function(d) New FileInfo(d).CreationTime)(0)

Hello @dlqhqo98 ,

You can refer to the below doc.

String.Join(“parameter”,Directory.GetFiles(FolderPath,”*”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(n))

You can use the index or using loop to take the filenames.

thank you. Thanks, it worked out fine.

I have one more question.

If the file name includes a date (Filname_2022-05-24.xlsx), I would like to select only files created from 24 hours ago to now.
Is there any way?

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