• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1210
  • Last Modified:

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

0
lynmke
Asked:
lynmke
  • 7
  • 6
  • 4
3 Solutions
 
Ess KayEntrapenuerCommented:
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
0
 
lynmkeAuthor Commented:
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
0
 
Ess KayEntrapenuerCommented:
i think you might want to make some new folders, and divide your 5000 files :/
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Ess KayEntrapenuerCommented:
there are applications which can batch move files with same prefix


heres an answer to that question

http://www.a-pdf.com/faq/how-do-you-batch-move-same-prefix-pdf-files.htm
0
 
Ess KayEntrapenuerCommented:
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
0
 
lynmkeAuthor Commented:
:) esskayb2d, if you run my 'long' code above its actually not that bad, just abit of tweaking....by you genius!
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
Are you abandoning your other identical question?

I assume your last example has a typo, i.e.,

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

should be merged into

9721234.pdf

not

721234.pdf

Since the number of characters in the "similar prefix" varies (9 in the first example, 7 in the second and third examples), the length of the prefix can't be used as the criterion for matching. So I'm thinking that perhaps a list of known delimiters can be used. In your examples above, the delimiters are space and underscore (hyphen is not a delimiter, otherwise the second example would be merged into <12.pdf>, not <12-1234.pdf>). Do you have a list of known delimiters? Also, are there ever two delimiters in the same "similar prefix"? For example, is there anything like this:

9721234_Add DSE.pdf
9721234_Add SED.pdf

If so, should these be merged into

9721234.pdf

or

9721234_Add.pdf

Regards, Joe
0
 
lynmkeAuthor Commented:
Hi esskayb2d,

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

Thanks,

lyn
0
 
Ess KayEntrapenuerCommented:
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
0
 
lynmkeAuthor Commented:
Thanks esskayb2d,

Yup, I found and edited a batch file that created the new folders to include multiple similar filenames into the folder.

@ECHO OFF
SETLOCAL
SET "sourcedir= C:\Users\test_2"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
 'dir /b /a-d "??????_* *.*"'
 ) DO (  
MD %%a
MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF

Next, I'll run this Merge tool and keep you posted!

-lyn
0
 
lynmkeAuthor Commented:
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!
0
 
Ess KayEntrapenuerCommented:
glad to have helped. thanks for the points
0
 
lynmkeAuthor Commented:
Thanks Esskayb2d, I appreciated your help, which worked partly. Please ignore my comment.
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
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
0
 
lynmkeAuthor Commented:
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!
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
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
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
Netminder,
Thanks for taking this action — much appreciated! Regards, Joe
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now