Get file with newest week number from folder

I have a folder with a few files like this:

The beginning is always the same but the weeknumber isn’t. What I want is to get the file with the highest/ newest weeknumber. In this case the file with the weeknr. 30.

Does anyone have an idea to do this?

@Loons Loop over all files in a folder and get the number using regex \d from file name and compare for the greatest value.

@Arpit_Kesharwani Sorry I’m not really sure how I should do it. :confused:

I found out that the file with the highest week number is also the newest file in the folder. So I found another topic with the following code:

outfilepath =
Directory.GetFiles(“C:\Users” + Environment.UserName + "\MyFilePath\Example WK ", “*.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime)(0)

I tried this with a test file and it works. But if I change the path to the file I need I just get an error.

@Loons under get files () replace with your folderpath and that’s it

Inserted my Path like this:

But there’s still an Error


Okay I changed the folder path and deleted the name of the file. Now my folder path looks like:
“C:\Users”+ Environment.UserName + “\Documents\Upload”

I don’t get an Error anymore but the Output is always the file with weeknumber 24. Even if the file with wnr. 30 is newer. I always get file wnr.24

Any idea why this happens?

@Loons basically the code which you are using for file creation descending order not for the file name ending with number. It is giving you the latest file created in the folder