I am retrieve the latest file from a directory however it retrieves the one prior to it

Dear Friends,

Good Day.

I am retrieve the latest file from a directory however it retrieves the one prior to it.

Directory.GetFiles(myfolder,"*.xlsx").OrderByDescending(Function(d) New FileInfo(d).CreationTime).ToList(0)

image

Need your support as always to resolve the same.

Regards,
MAnjesh

Hi,

Can you try LastWriteTime property instead of CreationTime?

Regards,

Hello @Yoichi

I still have the same issue.

Directory.GetFiles(myfolder,"*.xlsx").OrderByDescending(Function(d) New FileInfo(d).LastWriteTime).ToList(0)

image

Regards,
Manjesh

Hi,

How about using System.IO.File.GetLastWriteTime property?

Regards,

Dear @Yoichi

How can I pass the directory and file types. If you can give the complete syntax that would be great.

Regards,
Manjesh

Hi,

Can you try the following?

Directory.GetFiles(myfolder,"*.xlsx").OrderByDescending(Function(d) System.IO.File.GetLastWriteTime(d)).ToList(0)

Regards,

Dear @Yoichi ,

Still the same issue it is not fetching the last file, it is fetching the 1 before it.

hint: I am writing this file couple of seconds ago before accessing it. I am also using onedrive

Regards,
Manjesh

Hi manjesh_kumarmanjesh kumar,

I am using this flow for fetching the latest file.
You may refer to the attached file.
It might not the best solution but hopefully it can give you some idea.

Regards,

GetLatestFile.zip (34.4 KB)

Hello @hairulazizan ,

I am calling this get files at least 2 to 3 times in my workflow and I would not like to use the for each each and every time.

Thank you very much for letting your inputs.

I am wondering why it is not picking the latest file. Any thoughts from the RPA masters.

I also faced one more issue when the directories are empty.

KIA_GR: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Directory.GetFiles(myfolder,"*.xlsx").OrderByDescending(Function(d) System.IO.File.GetLastWriteTime(d)).ToList(0)

Regards,
Manjesh

Hi,

It might be cache matter.
Can you try the following sample?

Sequence.xaml (6.3 KB)

I also faced one more issue when the directories are empty.

If directory is empty, list size will be zero. So you need to check list size if it is 1 or more before get first item.

Regards,

Hello @Yoichi ,

It was my mistake that I was initializing this variable at the beginning of the program and that was the reason it was fetching the latest file at that point of time.

Now I am fetching the latest file just before where it is required and works fine. Thank you all for the support.

Regards,
Manjesh

1 Like

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