Sort Array of files in ascending order

Hello all,

I have a folder with images saved as 0.png , 5.png, 10.png, 15.png

I need to read the files in an array of ascending order and add them in a for each loop for a process.

Kindly help.

Thanking you


Directory.GetFiles(“folderPath”).OrderByDescending(Function(x) x.LastWriteTime).ToArray


test.xaml (7.1 KB)

  1. assign Directory.GetFiles("C:\Users\jackc\Desktop\Work\Test").ToList to filesList (list of string variable)

  2. sort filesList by assigning
    filesList.AsEnumerable.orderby(function(x) cint(Path.GetFileNameWithoutExtension(x))).tolist to filesList


we assume that the files should be ordered by th number form the filename
We also would like to mention, that a lexically ordering will be false ordering
we see the 10 befor the 2

So lets bring the number from the filename to an int, and use it for the ordering

arrFilesOrdered =

(From f in Directory.GetFiles(YourPath,"*.png")
Order by CInt(Path.GetFileNameWithoutExtension(f))
Select x = f).toArray

Kindly note: when filename is different eg. 0ABC.png then we do have adapt the statement as it is expecting a number only in the part of filename without the fileextension

Not able to get “LastWriteTime”

when interested on the last write time

Directory.GetFiles(“folderPath”).OrderByDescending(Function(x) New FileInfo(x).LastWriteTime).ToArray

[CheatSheet] - Filesystem APIs - News / Tutorials - UiPath Community Forum

test.xaml (7.2 KB)

i just changed the sorting to this
filesList.AsEnumerable.orderby(function(x) cint(System.Text.RegularExpressions.Regex.Match(Path.GetFileNameWithoutExtension(x), "([\d]+)ms").Groups(1).Value)).tolist


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