Sort alpha numeric file names in studio X

Hello,

I have pdf files in folder with names as

P1,P2,P11,P3,P2a,V1,V2,V22,V3,VSO

I need to merge/join these pdf files.

But if i merge them the order shows as

P1,P11,P2,P3,P2a,V1,V2,V22,V3,VSO

Need assistance in sorting the file in alpha numeric order using UiPath STUDIO X

P1,P2,P2a,P3,P11,V1,V2,V3,V22,VSO

@mihir.s.bandodkar

  1. Use a for each file in folder and in that select sort by name
  2. Inside the loop use merge files activity…so that the files are merged in the required name order

Cheers

Hi @mihir.s.bandodkar ,
You can use for each file in folder
image
in order by select name ascending first, filter by “.pdf”
inside for loop, use merge PDFs
hope it help,
regards,

Hi @mihir.s.bandodkar

  1. Use the “Set Variable Value” activity to get the list of PDF file paths in the folder and sort them using a custom sorting expression. Here’s the code to sort them in alphanumeric order:
Directory.GetFiles("YourFolderPath", "*.pdf").OrderBy(Function(f) New String(f.Where(Function(c) Char.IsLetterOrDigit(c)).ToArray())).ToArray()

Replace “YourFolderPath” with the actual path to your folder containing the PDF files.

  1. Use a “For Each” activity to loop through the sorted file paths.

  2. Inside the “For Each” loop, use a “Merge PDF Files” activity to merge the PDF files one by one.

This workflow will ensure that the PDF files are sorted in alphanumeric order before merging them.

Hope it helps!!

Hello,

Thanks for the response, i tried the solution and its not working, Below are the file names of pdf files which i am looking to merge in alpha numeric order.

Would really appreciate if you can sort.

File Names Should be Sorted as
P85.pdf P0
v102.pdf P1
v84.pdf P1a
v14.pdf P2
v59.pdf P3
P22.pdf P4
P63.pdf P5
v87.pdf P6
P32.pdf P7
P94.pdf P8
v71.pdf P9
P135.pdf P10
P77.pdf P11
P91.pdf P12
v86.pdf P13
v60.pdf P14
P65.pdf P15
v105.pdf P16
P10.pdf P17
v47.pdf P18
v101.pdf P19
P12.pdf P20
v100.pdf P21
P19.pdf P22
P26.pdf P23
P45.pdf P24
v67.pdf P25
v97.pdf P26
P35.pdf P27
v92.pdf P28
v91.pdf P29
v82.pdf P30
v104.pdf P31
P133.pdf P32
P69.pdf P33
P57.pdf P34
v18.pdf P35
P4.pdf P36
P2.pdf P37
P72.pdf P38
p37.pdf P39
v37.pdf P40
v6.pdf P41
P31.pdf P42
v54.pdf P43
P132.pdf P44
P16.pdf P45
P55.pdf P46
P128.pdf P47
v50.pdf P48
v93.pdf P49
v88.pdf P50
v109.pdf P51
P24.pdf P52
P76.pdf P53
P97.pdf P54
P0.pdf P55
P15.pdf P56
v61.pdf P57
v79.pdf P58
P123.pdf P59
P66.pdf P60
P98.pdf P61
P7.pdf P62
P74.pdf P63
v69.pdf P64
P39.pdf P65
P114.pdf P66
v98.pdf P67
P48.pdf P68
v36.pdf P69
P52.pdf P70
v29.pdf P71
v20.pdf P72
P80.pdf P73
v90.pdf P74
v38.pdf P75
v81.pdf P76
P18.pdf P77
v110.pdf P78
P105.pdf P79
v85.pdf P80
v73.pdf P81
v39.pdf P82
v57.pdf P83
v72.pdf P84
P59.pdf P85
v43.pdf P86
v13.pdf P87
v41.pdf P88
P62.pdf P89
P109.pdf P90
v40.pdf P91
v65.pdf P92
P92.pdf P93
P125.pdf P94
v76.pdf P95
P41.pdf P96
P112.pdf P97
P75.pdf P98
v1.pdf P99
P110.pdf P100
P36.pdf P101
v66.pdf P102
v63.pdf P103
P60.pdf P104
v112.pdf P105
v19.pdf P106
P95.pdf P107
P17.pdf P108
P30.pdf P109
P56.pdf P110
v78.pdf P111
P131.pdf P112
v33.pdf P113
P86.pdf P114
P115.pdf P115
v113.pdf P116
v22.pdf P117
P104.pdf P118
P21.pdf P119
P107.pdf P120
P5.pdf P121
P8.pdf P122
v8.pdf P123
P101.pdf P124
vso.pdf P125
P70.pdf P126
v12.pdf P127
P134.pdf P128
v49.pdf P129
P53.pdf P130
P11.pdf P131
P100.pdf P132
P6.pdf P133
P34.pdf P134
v30.pdf P135
v46.pdf V1
v56.pdf V2
P87.pdf V3
v48.pdf V4
v16.pdf V5
P47.pdf V6
P1.pdf V7
v11.pdf V8
P13.pdf V9
P84.pdf V10
v89.pdf V11
v62.pdf V12
P61.pdf V13
P1a.pdf V14
v51.pdf V15
v80.pdf V16
P9.pdf V17
v5.pdf V18
P54.pdf V19
P68.pdf V20
v9.pdf V21
P44.pdf V22
v15.pdf V23
v24.pdf V24
P38.pdf V25
P88.pdf V26
v77.pdf V27
P46.pdf V28
P106.pdf V29
P51.pdf V30
v99.pdf V31
v7.pdf V32
v55.pdf V33
P129.pdf V34
v108.pdf V35
v96.pdf V36
v26.pdf V37
P122.pdf V38
P20.pdf V39
v32.pdf V40
P126.pdf V41
v44.pdf V42
v31.pdf V43
v2.pdf V44
v27.pdf V45
v83.pdf V46
P58.pdf V47
v3.pdf V48
v103.pdf V49
P71.pdf V50
v68.pdf V51
P40.pdf V52
P27.pdf V53
P25.pdf V54
P113.pdf V55
P108.pdf V56
P14.pdf V57
P81.pdf V58
v64.pdf V59
v95.pdf V60
P99.pdf V61
v17.pdf V62
P67.pdf V63
v42.pdf V64
P117.pdf V65
v75.pdf V66
P90.pdf V67
v94.pdf V68
v23.pdf V69
P93.pdf V70
v107.pdf V71
P64.pdf V72
v58.pdf V73
v21.pdf V74
P103.pdf V75
v53.pdf V76
v45.pdf V77
P42.pdf V78
P73.pdf V79
P121.pdf V80
P102.pdf V81
P116.pdf V82
P82.pdf V83
P130.pdf V84
P124.pdf V85
P50.pdf V86
P28.pdf V87
P120.pdf V88
v74.pdf V89
v52.pdf V90
v35.pdf V91
v34.pdf V92
P127.pdf V93
P43.pdf V94
P119.pdf V95
v10.pdf V96
P23.pdf V97
v28.pdf V98
P96.pdf V99
v25.pdf V100
v111.pdf V101
v70.pdf V102
v106.pdf V103
P79.pdf V104
v4.pdf V105
P33.pdf V106
P118.pdf V107
P49.pdf V108
P78.pdf V109
P89.pdf V110
P3.pdf V111
P111.pdf V112
P83.pdf V113
P29.pdf VSO

Hi @mihir.s.bandodkar

  1. Use the “For Each File in Folder” activity to loop through the PDF files in your folder.

    • Set the folder path to the folder containing your PDF files.
    • Use the “*.pdf” filter to only process PDF files.
  2. Inside the loop, use an “Assign” activity to create a List variable to store the file names without extensions:

    • Assign fileNamesList as New List(Of String)()
  3. Use an “Add To Collection” activity inside the loop to add the current file name (without extension) to the fileNamesList:

    • Item: Path.GetFileNameWithoutExtension(item.ToString)
    • Collection: fileNamesList
  4. After the loop, use an “Assign” activity to sort the fileNamesList in alphanumeric order:

    • Assign sortedFileNames as fileNamesList.OrderBy(Function(x) New String(x.Where(Function(c) Char.IsLetterOrDigit(c)).ToArray())).ToList()
  5. Use the “For Each” activity to loop through the sortedFileNames.

  6. Inside the second loop, use the “Merge PDF Files” activity to merge the PDF files one by one, using the current sorted file name with an extension:

    • PDF File: "YourFolderPath\" + item.ToString + ".pdf"

Hope it helps