Link to home
Start Free TrialLog in
Avatar of lynmke
lynmke

asked on

Merge PDF files for pdf files with same prefix in filename

Please help to merge over 5,000 pdf files aumotically.

I have Adobe Acrobat Standard XI, and I have 5,000 pdf files that contain specific formats as below.
 
I want to merge the .pdf files that contain similar prefix into one pdf file.

K11200235 Support.pdf
K11200235 Additional.pdf
( there is always a space between the numbers and text on the 1st two examples below);

to merge into K11200235.pdf
or

12-1234 Support.pdf
12-1234 Additional.pdf

to merge into 12-1234.pdf

or

9721234_Add-DSE.pdf
9721234_Add-SED.pdf

to merge into 721234.pdf.

I found some code below, that seems like it would do the job, howeer, I am getting an error in line 44.  ( mismatched type: Ubound)

i do not script but am willing to try to avoid the manual merge process:)

---

Set fso = CreateObject("Scripting.FileSystemObject")

sFolder = "C:\test\"
dFolder = "C:\test\final"
 Set oFolder = fso.GetFolder(sFolder)
Dim file_group

'Sort the list in the Array name.
 'listArray = SortedFiles(oFolder)
 'listArray = SortedFiles(sFolder)
 file_names = SortedFiles(sFolder)
 
'msgbox "file_names : " & file_names(1)
 
'listArray = Quicksort(file_names, 1, oFolder.Files.Count)
 listArray = Quick_Sort(file_names, 1, oFolder.Files.Count - 1)
 
'msgbox "testa " & listArray(0) & " testb " & listArray(1)
 
f_filename = ""
 l_filename = ""
 'file_group(0) = ""
 'msgbox uBound(listArray)
 For x = 0 To UBound(listArray)
 f_filename = listArray(x)
 i = x + 1
MsgBox "listArray " & listArray(i)
 Do While InStr(1, listArray(i), f_filename, 1) > 0
ReDim Preserve file_group(i)
 file_group(i) = listArray(i)
 i = i + 1
 MsgBox "Step1"
 Loop
 x = i
MergePDFFiles (file_group)
 
ReDim file_group(0)
 
Next
MsgBox "Done"
 
Function MergePDFFiles(ByRef pdf_files)
bFirstDoc = True
recs = UBound(pdf_files)
 If recs < 2 Then
'If oFolder.Files.Count < 2 Then
' MsgBox "needed 2 pdf."
Set oMainDoc = CreateObject("AcroExch.PDDoc")
oMainDoc.Open sFolder & "\" & f_filename & ".pdf" 'oFile.Name
oMainDoc.Save 1, dFolder & f_filename & ".pdf"
oMainDoc.Close
Exit Function
End If
'For Each oFile In oFolder.Files
For i = 0 To UBound(pdf_files)
 MsgBox "MergePDFFiles"
 If bFirstDoc Then
bFirstDoc = False
Set oMainDoc = CreateObject("AcroExch.PDDoc")
oMainDoc.Open sFolder & "\" & f_filename & ".pdf" 'oFile.Name
Else
Set oTempDoc = CreateObject("AcroExch.PDDoc")
oTempDoc.Open sFolder & "\" & pdf_files(i) & ".pdf"
oMainDoc.InsertPages oMainDoc.GetNumPages - 1, oTempDoc, 0, oTempDoc.GetNumPages, False
oTempDoc.Close
End If
Next

oMainDoc.Save 1, dFolder & f_filename & ".pdf"
oMainDoc.Close
oTempDoc.Close
 'MsgBox "ok"

End Function

' Return an array containing the names of the
 ' files in the directory sorted alphabetically.
 Function SortedFiles(dir_path)
 Dim file_names
 Set fso = CreateObject("Scripting.FileSystemObject")

' Get the FSO Folder (directory) object.
 Set fso_folder = fso.GetFolder(dir_path)
 
' Make the list of names.
 ReDim file_names(fso_folder.Files.Count)
'msgbox "filecount " & fso_folder.Files.Count
i = 0
 For Each fso_file In fso_folder.Files
 'MsgBox "SortFiles"
 file_names(i) = Mid(fso_file.Name, 1, Len(fso_file.Name) - 4) 'File name minus the extension.
 i = i + 1
 ntemp = file_names(i)
 'MsgBox i & " " & ntemp
Next 'fso_file
 
' Sort the list of files.
 'Quick_sort file_names, 1, fso_folder.Files.Count
 
' Return the sorted list.
 SortedFiles = file_names
 
End Function
 
Function Quick_Sort(ByRef SortArray, ByRef First, ByRef Last)
 'Dim Low As Long, High As Long
 'Dim Temp As Variant, List_Separator As Variant
 Dim List_Separator
Low = First
 High = Last
 'msgbox "QuickSorta " & SortArray(0) & "QuickSortb " & SortArray(1)
 List_Separator = SortArray((First + Last) / 2)
 Do
 Do While (SortArray(Low) < List_Separator)
 Low = Low + 1
 Loop
 Do While (SortArray(High) > List_Separator)
 High = High - 1
 Loop
 If (Low <= High) Then
 Temp = SortArray(Low)
 SortArray(Low) = SortArray(High)
 SortArray(High) = Temp
 Low = Low + 1
 High = High - 1
 End If
 Loop While (Low <= High)
 If (First < High) Then Quick_Sort SortArray, First, High
 If (Low < Last) Then Quick_Sort SortArray, Low, Last

'msgbox "ArrayCount: " & UBound(SortArray)
'For i = 0 To UBound(SortArray)
 ' msgbox "fortest: " & SortArray(i)
 'Next

'Return the sorted list
 Quick_Sort = SortArray

End Function

Open in new window

Avatar of Ess Kay
Ess Kay
Flag of United States of America image

instead of using the filenames, use the folder names, merge everything inside it



have you tried this:  

Introduction
This complete Windows application lets you merge image and PDF files in a given folder into one PDF file. It also lets you password protect the PDF file. It uses free iTextSharp library.

Using the code
To use this program, simply select a folder and click Process. The program will create a PDF file within each folder and subfolder. The file will have the same name as the folder plus the PDF extension.
http://www.codeproject.com/Articles/69177/PDF-Merge
Avatar of lynmke
lynmke

ASKER

Thanks esskayb2d,

One slight problem is  that i have 5,000 pdf files in one folder. So there is no 'multiple folders'.

I need to merge these .pdf files, with the ones with multiple prefix filenames, as one combine file etc. Ihave Adobe Acrobat Standard XI.

What do you think?

Lyn
i think you might want to make some new folders, and divide your 5000 files :/
SOLUTION
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can also write a small program to do it for you.



something like -

for each filename in directory
    if folder exists (with x characters)
        move to folder
    else
        create folder
        move to folder
    end if
end loop
Avatar of lynmke

ASKER

:) esskayb2d, if you run my 'long' code above its actually not that bad, just abit of tweaking....by you genius!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lynmke

ASKER

Hi esskayb2d,

The Merge.pdf program did not create new folders, I thought that it would. Any ideas?

Thanks,

lyn
it just merges all pdfs inside one folder as a new file FOLDERNAME.pdf, as stated in the previous post

it does not create new folders, is simply browses the current folders and create files either inside them or on the root directory


if you can write code, download the solution and create an algorithm inside it to select files by prefix
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lynmke

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for lynmke's comment #a39804097

for the following reason:

Really only 2 people bothered!
glad to have helped. thanks for the points
Avatar of lynmke

ASKER

Thanks Esskayb2d, I appreciated your help, which worked partly. Please ignore my comment.
Hi _alias99,
The author just said, "Thanks Esskayb2d, I appreciated your help, which worked partly. Please ignore my comment." I assume the comment to be ignored is, "Really only 2 people bothered!" Being one of those two people, I recommend your choice #3:

Accept one or more Expert posts as the answer

However, I don't have a clue which post(s) "worked partly", so we need the author to jump back in and let us know. Also, I'm still curious how this question is any different from the previous one, where my answer was the Accepted Solution, although I am not suggesting that my one post on this question be the Accepted, or even Assisted, Solution...I'm fine with this one going totally to esskayb2d. Regards, Joe
Avatar of lynmke

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for lynmke's comment #a39804097

for the following reason:

This part worked perfectly. Thanks!
Hi lynmke,
Your comment in the close request (which I just objected to) said, "This part worked perfectly. Thanks!" I'm sure this comment of yours refers to one (or more) of esskayb2d's posts. Please find esskayb2d's post(s) that "worked perfectly" and mark it (or them) as the Accepted and/or Assisted solutions, giving it (or them) the 500 points, rather than accepting your own comment as the solution. Thanks, Joe
Netminder,
Thanks for taking this action — much appreciated! Regards, Joe