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?
@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 .
Just remove If filelist.count action and end action as highlighted below and keep the merge action. it will work as you expected.
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 🚀
@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?
Please find the below approach:
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 🚀
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
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!
eetuRobo
18
Super User 2025 Season 1
Nived_Nambiar
14
Super User 2025 Season 1
stampcoin
6