[HELP]Get the latest version of the named file

Hello,

can you help me? I have the list below and as you can see the files named "SUG deMIX"are listed as V1, v2, v3. I need to look for the latest version of the XLSX file, and it cannot be by date of modification but by the name of the file, taking into account the image below, the RPA would need to bring the file: “SUG de MIX DEZ V5-Final.xlsx”

Hi,

How about the following?

files = System.IO.Directory.GetFiles(yourFolderPath,"*.xlsx")

Then

files.GroupBy(Function(f) System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"(?<=\bV)\d+\b").Value).OrderByDescending(Function(g) CInt("0"+g.Key)).FIrst().OrderByDescending(Function(s) System.IO.Path.GetFileNameWithoutExtension(s).Contains("FINAL")).First()

Sample20221228-2L.zip (55.1 KB)

Regards,

Thanks for the quick response,

it will not be necessary to look for the string “FINAL” in the file, yes… in front of the listed files

V1
V2
V3
V4
V5

I need it to bring the highest number, in this example above: V5

Do you mean there is no multiple files contains “V5”? If so, the following will work.

files.OrderByDescending(Function(f) CInt("0"+System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"(?<=\bV)\d+\b").Value)).First()

Regards,

Sorry for not being able to clarify in a better way, in reality, there is only 1 of each file, V1,V2,V3. I need it to return the last number, for example: if it had V1 to V10, it needs to return the last file. “V10”

HI,

I just understood your requirement. Can you try the following expression?

"V"+(files.Select(Function(f) CInt("0"+System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"(?<=\bV)\d+\b",System.Text.RegularExpressions.RegexOptions.IgnoreCase).Value)).OrderByDescending(Function(i) i).First()).ToString

Regards,

it worked partially
how to bring the whole file name?
ex: SUG de MIX DEZ V5- FINAL.xlsx

Hi,

The above expressions :

 files.GroupBy(Function(f) System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"(?<=\bV)\d+\b").Value).OrderByDescending(Function(g) CInt("0"+g.Key)).FIrst().OrderByDescending(Function(s) System.IO.Path.GetFileNameWithoutExtension(s).Contains("FINAL")).First()

OR

files.OrderByDescending(Function(f) CInt("0"+System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"(?<=\bV)\d+\b").Value)).First()

returns filename with full path.

if it’s unnecessary path part, please use System.IO.Path.GetFileName method.

And if you need to get max version like “V5” and filename, first get filename, next extract version number from it as the following.

targetFileNameWithPath = files.GroupBy(Function(f) System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"(?<=\bV)\d+\b").Value).OrderByDescending(Function(g) CInt("0"+g.Key)).FIrst().OrderByDescending(Function(s) System.IO.Path.GetFileNameWithoutExtension(s).Contains("FINAL")).First()

targetFileName = System.IO.Path.GetFileName(targetFileNameWithPath)

strVersion = System.Text.RegularExpressions.Regex.Match(targetFileName,"\bV\d+\b",System.Text.RegularExpressions.RegexOptions.IgnoreCase).Value

Regards,

Thank you very much for your patience, solution and explanation.

1 Like

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