Get latest file based on creation time

Hello all.
I am trying to get the latest file and using the below code.

new DirectoryInfo(“C:\Users\Dinesh\Documents\UiPath\Excel_Automation\Input\Log”).GetFiles().OrderByDescending(Function(f) f.LastWriteTime).First()

But I am not getting the wrong file.

I am expecting the file 20230206_203506_SRC_PARK_ACC_FV60_Result.log from the above screenshot.

HI @Dinesh_Babu_S

Check out the expression

Directory.GetFiles("C:\Users\Dinesh\Documents\UiPath\Excel_Automation\Input\Log").[Select](Function(x) New FileInfo(x)).Where(Function(f) f.Extension.Equals(".txt")).OrderByDescending(Function(x) x.LastWriteTime).Take(1).ToArray()(0).ToString 

Regards
Gokul

1 Like

Thank you for responding.
It didn’t work.


Directory.GetFiles(“SRC_Common\Log”).[Select](Function(x) New FileInfo(x)).Where(Function(f) f.Extension.Equals(“.txt”)).OrderByDescending(Function(x) x.LastWriteTime).Take(1).ToArray()(0)

I just removed .tostring to rectify the error.

HI @Dinesh_Babu_S

Check the properties of the file

Regards
Gokul

1 Like

Yes.

Hi Dinesh,

Please try ‘For each file in folder’ activity. In this activity update “ORDER BY” => “Create date oldest first”.

2 Likes

@Ashutosh.Gupta It worked ,thank you.

1 Like

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