Sorting files in directory by section of file name


I have a list of files in a directory. I want to sort them based on a section of their name. All files have the same format where the x could be any amount of digits. How would i go about sorting them based on what is in the second set of parenthesis?


I was looking to use regex but i am unsure of the syntax needed.


Thanks for the help

You can test with this pattern to get the digits in the second set:


Use it with Regex.Match:

System.Text.RegularExpressions.Regex.Match(filename, "(?<=\(.+\)\()\d+(?=\)\(.+\))").ToString

Not sure why that didnt work for me?


For, change flavor to ECMAScript. It will work in UiPath.

Okay thanks for that. i got it working. Just out of curiosity, if there was 4 sets of data within brackets. how would i get the 4th?

To get the 4th:


1 Like

Do you know how i would sort files in a file directory based on what i am returning from the regex?

If the could be sorted ascending/descending from this set would be great

To sort by the second set:

Directory.GetFiles("C:\Test").OrderBy(Function (f) CLng(System.Text.RegularExpressions.Regex.Match(f, "(?<=\(.+\)\()\d+(?=\)\(.+\))").ToString)).ToArray

To get descending order, just replace OrderBy() with OrderByDescending().

1 Like

Thank you for this. i am getting an error saying “conversion from string “xxxx” to type long is not valid”

Does the for each need to have a particular TypeArgument ?

The only time CLng() would fail, is if the matched value is not a number. This could happen if there’s a file in the directory that does not follow your pattern (xxxxx)(xxxxxx)(xxxx), in which case Regex.Match would return on empty string.

If you loop through the files, the TypeArgument should be String.

If i do not want to return just numbers but figures too?

I have the below. and square brackets instead of round

Directory.GetFiles("C:\Users").OrderBy(Function (f) CLng(System.Text.RegularExpressions.Regex.Match(f, “(?<=([.+]){3}[).+(?=])”).ToString)).ToArray

The square brackets have a different meaning in regex. If you want to match literal brackets you need to escape them with backslash. \[ \]

CLng can only converts numbers. I don’t know what you mean with figures since to me it means the same thing. Please provide examples.

You should only match digits \d if you want to use CLng(). Remove CLng() if you want to sort by any match in the 4th brackets (wether it’s digits or other characters).

Thank you. was able to sort these using the below. However they are only sorted in the assign activity when i view the properties panel. they dont sort themselves in file exlorer. do you know why this is?

OrderBy(Function (f) (System.Text.RegularExpressions.Regex.Match(f, “(?<=([.+]){3}[).+(?=])”).ToString)).ToArray

It will only sort the array in UiPath. It won’t change anything in the File Explorer.

Okay thank you. Is there a way of sorting them in file explorer?

If not i want to just loop through the array and perform an action on the sorted array

You can sort by column in the File Explorer but not by part of a file name.


how could i learn regex code any reference i need to be expert

To learn Regex, check out this thread:

1 Like