How to sort files when copied from one folder to another

Hello I have two files(A_01 and B_02) in a folder. After copying them successfully in another folder using for each loop, they get stored as B_02 and then A_01. How should I sort them so that I get A_01 first and then B_02

1 Like

Hey. The sorting method I usually use (mainly because it’s the easiest one to remember and find, atleast to me) is using LINQ/lambda

First you need to get the list of files like with .GetFiles(), then you can use .OrderBy() with it.
The inline code (and you can use this in anywhere like in an Assign or directly in the For Each loop.) will look like this:

That will order by all the filepaths, but if you want more sophisticated sorting, then you need to manipulate f inside the OrderBy()

I hope this helps.



Hey, I tried the code above. It still gives B_02 first and then A_01. You mentioned manipulating f inside OrderBy(), can you suggest something for that please?

I tested it on my end using the below code and it worked:

{"B_02","A_01"}.OrderBy(Function(x) x)

The array {} would be your list of filenames/paths.

You would only need to do this if you want to sort by, for example, only the numbers or dates in the filename. Then, that would look like this:

filelist.OrderBy(Function(f) CLng( System.Text.RegularExpressions.Regex.Replace( System.IO.Path.GetFileName(f), "[^0-9]", "") ) )

I used a Regex pattern to extract the Integers from the filename and it will sort by that. So you can do more with this as well.

If you need further help, please provide some code snippets, samples, or screenshots.



Could you please take a look at my code and let me know where am I making a mistake?Main.xaml (6.3 KB)

So after looking at it, I don’t see any problems. In fact, you don’t even need to Sort your filenames as the way you have it coded, it will Copy all the files regardless of the order.

So from my understanding, the order that you are looking at is instead in File Explorer. If that is reversed, then you need to have the right sorting set in File Explorer (this is not related to UiPath). Like, if it is sorted by Modified Date, then B_02 will be at the top and A_01 will be second; if you sort it by the name, then it will be sorted alphabetically.

See image of File Explorer for what I mean:

Sorry, if I misunderstood. There really isn’t any problems. Now however, if you are wanting to process the items in a particular order, then that’s when you will use the .OrderBy(); if you just want to copy all files, then you don’t need to do it in any order.

Also, “item” contains the full filepath, so you can use item in the “From” property, as shown in the image below:


Thank you, this really helped.

1 Like

I’ll also add if you copy them in reverse order, then the modified date will place A_01 at the top since it got copied last. You can do this by using .Reverse

Or you can OrderBy().Reverse, to do reversed of alphabetical order

or OrderByDescending

Thanks :slight_smile:

How to take the pdf files in the folder in ascending order


Try with this,

Directory.GetFiles("yourPath").OrderBy(Function(p) p.CreationTime).ToArray()

Order the files in name ascending order


Directory.GetFiles("yourPath").OrderBy(Function(p) p.Name).ToArray() 

Correct me if I’m wrong, but GetFiles will give you strings, so p.Name won’t work. Instead, you need to get the filename of the filepath string. You can do so with Path.GetFilenameWithoutExtension()

Using your solution it would be like this:
Directory.GetFiles("yourPath").OrderBy(Function(p) Path.GetFileNameWithoutExtension(p)).ToArray()

Regards @Sweety_Girl

Also, if you are wanting to order it by numerical values within the filename, then this will require that you extract the number and convert the value to a number within the OrderBy() function.

1 Like

This must be stored in string variable correct!!

I need the array of string, with all the files names in the folder(in name ascending oder)

That gives you an array of strings which you can store to a variable if you like.

1 Like

Hi. thanks for this code. it orders the items i have correctly inside an array. How would i get these array items returned into file explorer?

not sure what you are asking. Can you be more specific?