How to sort files when copied from one folder to another


#1

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


#2

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:
image

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.

Regards.


#3

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?


#4

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.

Thanks.


#5

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


#6

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:
image

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:

Regards.


#7

Thank you, this really helped.


#8

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
image


#9

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

EDIT:
or OrderByDescending
image


#10

Thanks :slight_smile: