Get Latest Excel File Created from a folder

My automation creates a excel file with current date & Time file name by using excel application scope, something like Transactions_15-03-2021_12_40.xlsx which has 3 sheets, now i would like to read this excel file and merge all 3 sheets into a new sheet to perform further operations. Automation is running 4 times a day means a new excel file every time.
My question is how to get excel file created by current automation so that further operations can be performed.
Please let me know if any issues in question understanding.

Hi @ramvashista85 - Please try this below code…this will give the recent file created in the folder.

StrFileName = Path.GetFileName(Directory.GetFiles(YourFolderPath,"*.xlsx").OrderByDescending(Function(d) New FileInfo(d).creationtime).First)

OR (If in case, the above code does not fetching the right file then use the below)

StrFileName = Path.GetFileName(Directory.GetFiles(YourFolderPath,"*.xlsx").OrderByDescending(Function(d) New FileInfo(d).LastWriteTime).First)

StrFileName is of String Type

2 Likes

thank you @prasath17.
I tried Directory.GetFiles(str_ExcelDataFilePath, ".xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime)(0)
it returns me latest file created, now i can use that file for further operations.

1 Like

@ramvashista85 - glad to know…please mark my post as solution , that will close this thread.

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