How to print the files in ascending order based on the integer value?

Hi there,
Input is
A_1_B.XLSX
B_0_C.XLSX
C_5_D.TXT
D_3_E.TXT
E_2_F.XLSX
I need to print the input in Ascending Order based on the Integer Value
OUTPUT:
B_0_C.XLSX
A_1_B.XLSX
E_2_F.XLSX
D_3_E.TXT
C_5_D.TXT

thanks in advance,
@ppr
@Yoichi
@ushu
@vishal.kp
@supermanPunch
@Anil_G
@rlgandu
regards .
@kmaddikatla

Hi @kmaddikatla

Please check on this thread

I hope it helps!!

Hi @kmaddikatla ,
I think you can get only numeric string then sort

Regards,
LNV

@kmaddikatla

Assign: folderPath = “C:\Path\To\Your\Folder”
Assign: filePaths = Directory.GetFiles(folderPath)

Assign: sortedFilePaths = filePaths.OrderBy(Function(path) Int32.Parse(System.Text.RegularExpressions.Regex.Match(Path.GetFileName(path), “\d+”).Value)).ToArray()

For Each filePath In sortedFilePaths
Log Message: filePath
End For Each

Hi

Directory.GetFiles(Fol_Path).OrderBy(Function(F) New Fileinfo(F).SizeInKB).ToArray

Try this

Hi @kmaddikatla

  1. Use the “Directory.GetFiles” activity to get a list of all files in the specified folder.
  2. Use the “Assign” activity to create a new List of String to store the filenames.
  3. Use a For Each loop to iterate through each file path in the list of files.
  4. Inside the For Each loop, use the “Path.GetFileName” method to extract the filename from the file path.
  5. Use regular expressions to extract the numeric value from the filename.
  6. Use the “Convert.ToInt32” method to convert the extracted numeric value to an integer.
  7. Use the “Add” method to add the filename and its corresponding numeric value to the List of String.
  8. After the loop, use the “Sort” method to sort the List of String based on the numeric values.
  9. Use another For Each loop to iterate through the sorted List of String.
  10. Inside this loop, you can perform any necessary actions with the sorted filenames.

Hope it helps!!

what is the variable type of sortedfilepaths?
is it string of array ?
@rlgandu
Thanks in advance
regards
@kmaddikatla

@kmaddikatla

It’s array

Hi @kmaddikatla
=> Use the below syntax in Assign activity: Mention your folder name.

filePaths= Directory.GetFiles("C:\Users\"+Environment.UserName+"\Documents\UiPath\4\Folder 1")

FilesArray= filePaths.OrderBy(Function(path) CInt(System.IO.Path.GetFileNameWithoutExtension(path).Split("_"c)(1))).ToArray

Both filePaths and FilesArray are of type Array(System.String)

=> Run a for each for FilesArray
Print a log message.

Refer the workflow for reference
Sequence22.xaml (9.8 KB)

Hope it helps!!

Hi @rlgandu ,
getting an error" Getfilename is not a member of string

Thanks in advance
regards ,
@kmaddikatla

Thank you so much @Parvathy ,
This is working

1 Like

You’re Welcome @kmaddikatla

Happy Automation

Regards,

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