Solved

Merge PDF files for pdf files with same prefix in filename

Posted on 2014-01-14
19
697 Views
Last Modified: 2014-03-03
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
Comment
Question by:lynmke
  • 7
  • 6
  • 4
19 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39780162
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
 

Author Comment

by:lynmke
ID: 39780279
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
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39780398
i think you might want to make some new folders, and divide your 5000 files :/
0
 
LVL 15

Assisted Solution

by:Ess Kay
Ess Kay earned 250 total points
ID: 39780411
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
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39780427
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
 

Author Comment

by:lynmke
ID: 39780530
:) esskayb2d, if you run my 'long' code above its actually not that bad, just abit of tweaking....by you genius!
0
 
LVL 51

Assisted Solution

by:Joe Winograd, EE MVE
Joe Winograd, EE MVE earned 250 total points
ID: 39780538
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
 

Author Comment

by:lynmke
ID: 39801928
Hi esskayb2d,

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

Thanks,

lyn
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:Ess Kay
ID: 39803573
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
 

Accepted Solution

by:
lynmke earned 0 total points
ID: 39804097
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
 

Author Comment

by:lynmke
ID: 39825271
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
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39825272
glad to have helped. thanks for the points
0
 

Author Comment

by:lynmke
ID: 39826445
Thanks Esskayb2d, I appreciated your help, which worked partly. Please ignore my comment.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 39883417
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
 

Author Comment

by:lynmke
ID: 39897531
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
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 39897532
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
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 39902071
Netminder,
Thanks for taking this action — much appreciated! Regards, Joe
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now