How to get the files from a folder in the same order it is saved?

I have 15 files in a folder. Saved as file1, file2, file3…file15. When I use for each, it is not selecting the files in the same order. Instead of same result, I am currently getting the below result.
Current Result:
file1
file10

file15
file2

file9

image
My file name is already in this format. Should I still concatenate? If yes, can you please give some examples. I am new to UIPath.

You might want to sort the file by LastWriteTime - see example enclosed.
SortDirFilesLastModified.xaml (6,6 KB)

Hi @Vignesh_Sahoo

If you want to get it in the same order of the file name, then you need to pad zeros before 1, 2, 3, ie.
instead of saving them as:
file1, file2, file3, … file11, file12, etc
you should save them as:
file001, file002, file003, … file011, file012, etc.
Once you save your files in this naming convention you can apply the filter in this manner:
Directory.GetFiles(“E:\New folder”).OrderBy( Function(x) New FileInfo(x).Name )


To pad zeros, you can try this:

Convert.ToString( integer_variable ).PadLeft( integer_variable_numberofzeros, Char.Parse(“0”) )


If you want to filter them by creation time, then try this:
Directory.GetFiles(“E:\New folder”).OrderBy( Function(x) New FileInfo(x).CreationTime )

Thanks @kunalhore2891 . The problem is I cannot make any changes in the file name as it is directly linked with other operations. And creation date or last modified time also doesn’t help here because some levels are done earlier than others. The only thing which will help me is sort by name. Maybe I can try what you said and at the end create another loop to rename the files as is.

Hi @Vignesh_Sahoo

For the file names you mentioned (level-242-1 types) I have created a xaml to read them in sequential manner. Check the xaml.

Sequential File Access.xaml (7.5 KB)

In the first assign activity provide the path of the folder which contains your files.

1 Like

@Vignesh_Sahoo is your problem solved?