How to find out the latest file from file name?

Guys,

I intend to search for the latest created Zip file from a folder by checking on their file names because all of them have the same standard naming convention like file_ddmmyyyy.zip

My folder now having these 3 zip files:

file_06112019.zip
file_08112019.zip
file_10112019.zip

from their names I can tell the last file (file_10112019.zip) is the one that I am looking for, so I hope the robot could help me to return me the file name.

Anyone could help me out?

Thanks!

Hi there @Victor_Victory

Seems like we need more details on the file names.

If two zip files with the same naming is downloaded then Windows will automatically append β€œ(1)” or more counters to the existing filename.

It is better to get the file path based on the latest file rather than going with the names.

CASE 1 : Only one zip file is downloaded per day

-> 1. Get all files from the directory. Split the name using the β€˜_’ as delimiter.
2. Parse exact the date using the string from the filename
3. Subtract DateTime.Now minus the date of file creation.

CASE 2 : Recommended

Use the following code -

YourFileArray  = System.IO.Directory.GetFiles("YourDownloadPathParent")
FinalArray = System.Array.Sort(files, New FileInfoComparer).ToArray
YourLatestFilePath = FinalArray(FinalArray.Length-1)

Every line above should come in an assign activity.

PS : Pardon if there are mistakes in Sysntax as I didnt check it in Uipath - typed from my mobile.

Regards :slight_smile:

1 Like

Try this:
Directory.GetFiles(β€œC:\Users”).OrderByDescending(Function(f) New FileInfo(f).LastWriteTime).First()

3 Likes