How to get excel file from a folder containing many files

I have a folder named “ABCD” which contains 10 excel file whose initial name is same only after an underscore it can be differentiated.
Ex:- File name:-abcd_180398
File name2:- abcd_180399
I want to read the data from the latest file, as files are updated daily.
Only the point to differentiate the file is the date of modification
How to do that??

Hi @Kunal_Jain

First, get all the file names from the specified folder into an array like this:

fileList = Directory.GetFiles("Your Folder Path")

Then, determine the latest file like this:

latestFile = fileList.OrderByDescending(Function(x) New FileInfo(x).LastWriteTime).FirstOrDefault()

This will get you the latest created file.

Hope this helps,
Best Regards.

1 Like

@Kunal_Jain

You can use this

Directory.Getfiles("Folderpath","abcd*.xlsx").OrderByDescending(function(x) New FileInfo(x).LastWriteTime).FirstOrDefault

Cheers

Hi @Kunal_Jain

This is the expression that’ll get you the latest one:

Directory.GetFiles(folderPath).[Select](Function(x) New FileInfo(x)).Where(Function(f) f.extension.equals(“.xlsx”)).OrderByDescending(Function(x) x.LastWriteTime).take(1).ToArray()(0).ToString()

Regards,

You can have order by set to Last Updated date newest first and then break the loop once you have process single file.
image

Hey Thanks for your Help!
This worked for me

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