Skip to main content

Notifications

Community site session details

Community site session details

Session Id : T1894NsuIwrPHqK4bNVcX3
Power Automate - Power Automate Desktop
Answered

merging pdfs

Like (0) ShareShare
ReportReport
Posted on 20 May 2024 15:05:40 by 74

Hello All,

I have a question. Is it possible to build a workflow that will combine PDF files with same first digit? for example these are some file names: 0048600218_2024-APR-U002M204, 0048600946_2024-APR-U002M2H4, 0048602798_2024-APR-U002M2I4. first five is 00486. If first five is same, I want to combine these PDFs. get files in a folder>>split text>> not sure what should i do next to cmbine. is it possible? 

  • Deenuji_Loganathan_ Profile Picture
    6,105 Super User 2025 Season 1 on 22 May 2024 at 13:08:05
    Re: merging pdfs

    @rrahat93 - It means a lot. Thank you. Happy Automation 🙂

  • RR-05041550-0 Profile Picture
    74 on 22 May 2024 at 12:43:37
    Re: merging pdfs

    @BG-PowerPlat Hello Power Platform Mr. @Deenuji  is a true MVP of power platform. and we as a user lucky to have him here.

    Thank you so much @Deenuji .

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,105 Super User 2025 Season 1 on 22 May 2024 at 05:12:29
    Re: merging pdfs

    @rrahat93 

     

    Just remove If filelist.count action and end action as highlighted below and keep the merge action. it will work as you expected.

     

    Deenuji_0-1716354736059.png

     

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

  • RR-05041550-0 Profile Picture
    74 on 21 May 2024 at 18:17:14
    Re: merging pdfs

    @Deenuji THANK YOU so much. It works, one more concern. So there are three different kind of files. 0048600218_2024-APR-U002M204, 0048600946_2024-APR-U002M2H4, 0048602798_2024-APR-U002M2I4. 001135_2024-APR-U002M204, 00137700218_2024-APR-U002M204. So, in the output file I was trying to get three files 00486, 00113,00137. Is it possible?

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,105 Super User 2025 Season 1 on 21 May 2024 at 16:38:13
    Re: merging pdfs

    @rrahat93 

     

    Please find the below approach:

    Deenuji_0-1716309422013.png

     

    Code:

     

     

     

    Folder.GetFiles Folder: $'''D:\\pdf''' FileFilter: $'''*pdf''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
    SET DTgroupedFiles TO { ^['FirstFiveDigits', 'FilePath'] }
    Scripting.RunDotNetScript Imports: $'''System
    System.Data
    System.IO
    System.Linq''' Language: System.DotNetActionLanguageType.CSharp Script: $'''DataTable dt = new DataTable();
     dt.Columns.Add(\"FirstFiveDigits\", typeof(string));
     dt.Columns.Add(\"FilePath\", typeof(string));
    
    foreach (string file in files)
     {
     string fileName = Path.GetFileNameWithoutExtension(file);
     string firstFiveDigits = fileName.Substring(0, 5);
    
     dt.Rows.Add(firstFiveDigits, file);
     }
    
     string currentGroup = null;
     string filePaths = null;
    
     foreach (DataRow row in dt.Rows)
     {
     string firstFiveDigits = row[\"FirstFiveDigits\"].ToString();
     string filePath = row[\"FilePath\"].ToString();
    
     if (currentGroup == null || currentGroup != firstFiveDigits)
     {
     if (currentGroup != null)
     {
     groupedFiles.Rows.Add(currentGroup, filePaths);
     }
     currentGroup = firstFiveDigits;
     filePaths = filePath;
     }
     else
     {
     filePaths += \",\" + filePath;
     }
     }
    
     if (currentGroup != null)
     {
     groupedFiles.Rows.Add(currentGroup, filePaths);
     }''' @'name:files': Files @'type:files': $'''List''' @'direction:files': $'''In''' @'name:groupedFiles': DTgroupedFiles @'type:groupedFiles': $'''Datatable''' @'direction:groupedFiles': $'''InOut''' @groupedFiles=> DTgroupedFiles
    LOOP FOREACH CurrentItem IN DTgroupedFiles
     Text.SplitText.SplitWithDelimiter Text: CurrentItem['FilePath'] CustomDelimiter: $''',''' IsRegEx: False Result=> FileList
     IF FileList.Count > 1 THEN
     Pdf.MergeFiles PDFFiles: FileList MergedPDFPath: $'''D:\\pdf\\%CurrentItem[0]%.pdf''' IfFileExists: Pdf.IfFileExists.AddSequentialSuffix PasswordDelimiter: $''',''' MergedPDF=> MergedPDF
     END
    END

     

     

     

    Just copy and paste the above code into you new flow power automate desktop and change your folder name path.

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

     

     

     

  • MichaelAnnis Profile Picture
    5,721 Super User 2025 Season 1 on 20 May 2024 at 20:57:36
    Re: merging pdfs

    Variables.CreateNewList List=> Dishes
    Folder.GetFiles Folder: ToProcessFolder FileFilter: $'''*.csv''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
    File.RenameFiles.RenameReplaceText Files: Files TextToReplace: $'''Dirty''' ReplaceWith: $'''Clean''' IfFileExists: File.IfExists.Overwrite RenamedFiles=> RenamedFiles
    LOOP FOREACH CurrentItem IN RenamedFiles
    Text.GetSubtext.GetSubtextFromStartTo Text: CurrentItem.Name NumberOfChars: 5 Subtext=> Subtext
    File.Move Files: CurrentItem Destination: $'''%MainFolder%\\%Subtext%''' IfFileExists: File.IfExists.DoNothing MovedFiles=> MovedFiles
    END

  • RR-05041550-0 Profile Picture
    74 on 20 May 2024 at 20:45:17
    Re: merging pdfs

    Hello @MichaelAnnis can you please give me an example?

  • MichaelAnnis Profile Picture
    5,721 Super User 2025 Season 1 on 20 May 2024 at 17:03:02
    Re: merging pdfs

    There is a "Merge PDF files" action.  You will need to build out the Merged PDF path with the name that you want.  Such as %ResultFilePath%_%Subtext% where ResultFilePath is the folder you want to save it in and %Subtext% is the first 5 that you pulled.

     

    Is there any other action you need to with these files such as move/copy them.  To make the logic easiest, I would put these files in a folder by themselves and then merge the entire folder; this will stop you from having to create lists and creating logic around managing those lists or what if it should have already been processed etc.

    Get files from processing folder
    Create list - subfolders to be processed
    For each file
        get subtext
        move file to subprocessing temp folder (based on first five, create folder if it doesn't exist; add created subfolder to subfolders list)
    end for each

    for each subfolder
        get files
        merge files
    end for each

    good luck!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Power Automate Desktop

#1
eetuRobo Profile Picture

eetuRobo 18 Super User 2025 Season 1

#2
Nived_Nambiar Profile Picture

Nived_Nambiar 14 Super User 2025 Season 1

#3
stampcoin Profile Picture

stampcoin 6

Overall leaderboard