Uipath sort file names based on alpha numberic names

Have a folder containing various alpha numeric files,

A1, A2, A10, A23, B1, B11, B3, B34

when i use the sort data table option the files are getting sorted as below.

A1, A10, A2, A23, B1, B11, B3, B34

Can someone help to sort the file names in alpha numeric way.

A1, A2, A10, A23, B1, B3, B11, B34

Hi @mihir.s.bandodkar

Try this

(From x In Directory.GetFiles(“YourPath”, “*.pdf”)
Let fname = Path.GetFileNameWithoutExtension(x)
Let shlp = New String(fname.Where(Function(s) Char.IsDigit(s) OrElse s.Equals(“.“c)).ToArray)
Let arrSplit = shlp.Split(”.“c)
Let arrSort = If(arrSplit.Length = 2, arrSplit, {shlp,”-1”})
Order By CInt(arrSort(0)), CInt(arrSort(1))
Select f=x).toArray)

Give file extension instead of pdf

  1. Add a Assign activity to define the folder path you want to sort. Set the variable “folderPath” to the desired folder path.
  2. Use an Assign activity give the below syntax: Variable:filePath(Array of string)

filePath= DIrectory.GetFiles(folderPath)

  1. Add an Assign activity to store the sorted file names. Create a new variable, let’s call it “sortedFileNames”, use below syntax:
sortedFileNames = fileNames.OrderBy(Function(f) f, StringComparer.OrdinalIgnoreCase).ToArray()
  1. Add a For Each activity to iterate through the sorted file names, Set the TypeArgument property to String and provide “sortedFileNames” as the input.
  2. Inside the For Each loop, you can perform actions on each file. For example, you can display the file names or perform further operations on them.

Hope it helps!!

Regards,

Hi @mihir.s.bandodkar

  1. Read the file names into an array or List using the “Directory.GetFiles” activity.
  2. Add an Assign activity to sort the file names using LINQ. Name the variable “sortedFiles” (of type List(Of String) ) and use the following expression:

sortedFiles = files.OrderBy(Function(file) Integer.Parse(Regex.Match(file, “\d+”).Value)).ThenBy(Function(file) file)
.ToList()

This LINQ expression first sorts the file names based on the numeric part using OrderBy , and then sorts them alphabetically using ThenBy . The sorted file names are converted to a List using ToList()
Iterate through the sorted file names using a For Each activity and Within the For Each loop, you can perform the desired actions on each file name.

Thanks!!

Hello, Thanks for the response.

Thanks for your response.

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