Search the Latest Previous File in Folder

Hello,
I have a folder .

C:\RPA\Dev\Test
Contains some files Having the naming Fix format.
{FileName Date.xlsx}

TestFile 07-10-2022.xlsx
TestFile 07-09-2022.xlsx
TestFile 07-08-2022.xlsx

If todays Date is 07-13-2022.
I want to select the Latest Previous File.
In the above case it should select “TestFile 07-10-2022.xlsx” since it’s the latest Previous file for the Todays Date.

Should check for last 5 days.
07-12-2022 - not available
07-11-2022 - not available
07-10-2022 - Available - Select File - Exit loop OR break search
07-09-2022
07-08-2022

Any ideas or help would be appreciated.

Thanks.

Hi Gagan,

Read all the .xlsx files in the folder then sort it based on Date and pick the first one from the array
Files = Directory.GetFiles(filePath,“*.xlsx”)
Files.OrderByDescending(Function(s) Convert.ToDateTime(Path.GetFileNameWithoutExtension(s).Split(" "c)(1)) )(0)

Thanks

Thank you quick response
Will it check for 5 days ?

Can you please provide a sample flow ?

Thanks.

Hi Gagan,

This will not be specific to 5 days, it can take the latest previous file from any number of days and still return the latest previous file.
if there is no File in last 5 days, How do you want the response?

Thanks

What was the File name format you used.
Is it same - TestFile 07-10-2022.xlsx, TestFile MM-dd-yyyy.xlsx

Will throw an Exception “File not found for last 5 Days”

Yes this is same File Format I am using.

Thanks

GetLatestPreviousFile.xaml (7.4 KB)

Try this Sample Flow.
Just replace the DirectoryPath Variable with you actual folder Path.

Thanks.

Thanks a lot for this,
But getting the same error.

Sharing the actual name of the Files.
DateError

GetLatestPreviousFile.xaml (7.4 KB)

Try this updated Code, it should work.

Thanks
Abhishek

Thanks for all the Help but unfortunately same error.

GetLatestPreviousFile.xaml (8.0 KB)

Try this Code, I have modified it to handle the exact Date Format.

Thanks
Abhishek