web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Automate
Answered

merging pdfs

(0) ShareShare
ReportReport
Posted on 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? 

I have the same question (0)
  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    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!

  • RR-05041550-0 Profile Picture
    74 on at

    Hello @MichaelAnnis can you please give me an example?

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    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

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

    @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 🚀

     

     

     

  • RR-05041550-0 Profile Picture
    74 on at

    @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,250 Super User 2025 Season 2 on at

    @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 at

    @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 .

  • Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

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

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 501 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 323 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard