How to select and load the newest file from a folder?

Hi there,

This a process occurring once every month. At the beginning I need to select the newest file from a folder - they are named like: PT_2019_01, PT_2019_02,PT_2019_03,PT_2019_04…
As we are now in October 2019, I would need to select PT_2019_09.
Any tips?

Thanks!

Hi,

If you want to get the last edited file you can sort the files by File.GetLastWriteTime.

For your current setup if you want to receive this month -1 (_09) ?
You can search for files in said folder with desired search pattern:
Directory.GetFiles("C:\Folder\", "PT_2019_" + Now.AddMonths(-1).ToString("MM"))

br,
Topi

1 Like

@sspi1153

I guess below thread will help you.

Find latest File in a folder with respect to date Created - #12 by Dominic

3 Likes

Directory.GetFiles(“FOLDER PATH”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).First

Hope this helps!

1 Like

Thanks!

Thanks, this one works as well!

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