Link to home
Start Free TrialLog in
Avatar of gci_ee
gci_ee

asked on

Merge PDF's into single files but in multiple folders

We've got around 400 folders that have 2 PDF's in each that we want to merge into a single PDF.I have looked for a tool or script to do this and there are plenty out there but they only really allow you to point to 2 PDF's at a time (i.e i'd have to run these tools in each of the 400 folders), the number of folders is likely to increase so I really need a script that I can put in the top folder (with the 400 folders below it) and click run and it'll go into each folder, merge the 2 PDF's into a single PDF that we can name (the name will be the same in each folder to make it easier)
Does anyone have any ideas on either a tool to do this (free) or even better a script we can copy?
Cheers
(apologies if I've posted this in the wrong forum but couldn't find a scripting one)
Avatar of Sivaraj Essaki
Sivaraj Essaki
Flag of India image

PDFill PDF Editor may be helpful for your need.

https://www.pdfill.com/pdftool_merge.html

It can be done through command prompt so you can write a batch file but unfortunately the command prompt feature only available with registered version of PDFill PDF Editor.

User generated imageI will also let you know if any free tool.

Regards, Shiva
I should be able to find the time today or tomorrow to write a script that you will place in the top folder and will do exactly what you want. It will use only free tools. Two questions for you: (1) Are there other PDFs in the folders that you do not want in the merged file? In other words, does the script have to refer to two specific file names, such as <input1.pdf> and <input2.pdf> that appear in each folder, or is it OK to combine all PDFs in each folder (via an <*.pdf> wildcard technique) into the merged file? (2) Does the order of combining them matter? If so, how? By file name alphabetically? By date modified, oldest first? By date modified, newest first? Regards, Joe
Avatar of gci_ee
gci_ee

ASKER

That would be great,
1:There are only 2 PDF's in each folder that want merging (there will be a word doc in each folder though that doesn't want anything doing to it) but the 2 PDF's in each folder will have random names so a wildcard would be fine.
2:I think date modified first (i.e oldest one first) or just the top one in the folder first if that would help

Many Thanks
(1) I'll use the <*.pdf> wildcard. This will work perfectly under the circumstances, since it will get the PDF files that have random names, but will skip the Word files (and any other non-PDF files).

(2) The thing is, the concept of the "top one in the folder" depends on how the files are sorted, i.e., there is no absolute concept of "top one". If the files are sorted by file name ascending, then <abc.pdf> might appear to be the top one; if the files are sorted by file name descending, then <xyz.pdf> might appear to be the top one; if they are sorted by modified date ascending or descending, created date ascending or descending, size ascending or descending, etc., that will affect which file is shown as the "top one". I can merge them by any criterion you want (by sorting on the relevant field, ascending or descending). Regards, Joe
Avatar of gci_ee

ASKER

If you can merge them with the oldest modified first would that be ok ? (i.e the older PDF at the top of the merge)
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
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
Avatar of gci_ee

ASKER

Thanks Joe,
I'll get that tested and will let you know.

Many thanks for your time on this.
You're welcome. I added the ability to sort by modified date, ascending or descending. The UI now looks like this:

User generated imageRegards, Joe
Hi GCI,
Just a quick note to let you know that I completed the article (with the AutoHotkey script) and submitted it for publication. The EE Page Editors are usually very fast, so I expect that it will be published today or tomorrow...Monday at the latest. As soon as it's published, I'll post the URL here. In the meantime, I hope you've had a chance to install and test AutoHotkey and PDFtk. Cheers, Joe

Update: The article, How to Combine-Merge PDF Files in Many Subfolders, was just published:
https://www.experts-exchange.com/Web_Development/Document_Imaging/A_13315-How-to-Combine-Merge-PDF-Files-in-Many-Subfolders.html

Regards, Joe
Avatar of gci_ee

ASKER

Worked great. Thanks
You're welcome. I'm glad to hear that it worked well for you. Regards, Joe
Avatar of gci_ee

ASKER

Hi Joe,

Apologies for the late reply in this but the test we did worked fine but we've installed the 2 programs to the server where the issue is but when I drag the script you created into AutoHotkey it shows PDFTK.EXE not found. Do we need to install the programs to a different directory ?

Thanks
The code looks for <pdftk.exe> in:

C:\Program Files\PDF Labs\PDFtk Server\bin\

and in:

C:\Program Files (x86)\PDF Labs\PDFtk Server\bin\

If it doesn't find it there, it gives you a browse-for-file dialog so you may navigate to wherever it is. You don't really have to install PDFtk — it doesn't need anything in the registry or in any other folders. All that's required are two files in the same folder: <pdftk.exe> and <libiconv2.dll>. For example, it will work fine if you simply put those two files in the folder:

\\servername\sharedapps\pdftk\

You could also modify the script to avoid the navigation. Here's the current code:
pdftkexe:="notfound"
IfExist,C:\Program Files (x86)\PDF Labs\PDFtk Server\bin\pdftk.exe
  pdftkexe:="C:\Program Files (x86)\PDF Labs\PDFtk Server\bin\pdftk.exe"
IfExist,C:\Program Files\PDF Labs\PDFtk Server\bin\pdftk.exe
  pdftkexe:="C:\Program Files\PDF Labs\PDFtk Server\bin\pdftk.exe"
If (pdftkexe="notfound")
{
  MsgBox,4145,Error,
  (
pdftk.exe not found in Program Files\PDF Labs\PDFtk Server\bin\ or in
Program Files (x86)\PDF Labs\PDFtk Server\bin\
`nClick OK and navigate to it or click Cancel to exit
  )
IfMsgBox,Cancel
  ExitApp
FileSelectFile,pdftkexe,,,Navigate to pdftk.exe or click Cancel to exit, Executables (*.exe)
If (pdftkexe="" or pdftkexe="notfound" or ErrorLevel=1)
  ExitApp
}

Open in new window

You could delete all of that and make it something like this:
pdftkexe:="H:\sharedapps\pdftk.exe"

Open in new window

But remember to put <libiconv2.dll> in the same folder — <pdftk.exe> needs that DLL. Regards, Joe
Avatar of gci_ee

ASKER

That file IS in the same folder. If I double click on the Merge.AHK file it just shows me the text in there. It doesn't appear to do anything with it. If I drag the Merge.AHK file onto the AutoHotKey icon then it shows PDFTK not found ?
> If I double click on the Merge.AHK file it just shows me the text in there.

That's a completely different issue. That says that AutoHotkey is not associated with the AHK file type. Sounds as if some other program (a text editor, a browser, whatever) owns the AHK file type and is displaying the <Merge.AHK> file when you double-click it.

In any case, try these steps:

(1) Download the <Merge.ahk> file attached to this post. It's the same script as the one at the article with one change — it looks for <pdftk.exe> in the same folder as the AHK script.

(2) Compile <Merge.ahk> into an executable (<Merge.exe>) on any computer where the AutoHotkey compiler is installed. You do this by right-clicking on <Merge.ahk> and selecting Compile Script.

(3) Put <pdftk.exe>, <libiconv2.dll>, and <Merge.exe> (the executable created by compiling the new <Merge.ahk) in the same folder — doesn't matter where.

(4) Double-click <Merge.exe>.

That should do it. Regards, Joe
Avatar of gci_ee

ASKER

If I right click Merge.AHK there is no option to COMPILE SCRIPT?
AutoHotkey is in the same folder (as in the Green H icon and running in the system tray)
> If I right click Merge.AHK there is no option to COMPILE SCRIPT?

That means AutoHotkey is not properly installed, which is also why double-clicking on an AHK file doesn't run the script. Same problem — AutoHotkey is not installed.

To fix this, go to:
http://ahkscript.org/

Click the big blue Download button in the upper right. That will download a setup file called <AutoHotkey111403_Install.exe>. Run that file, select the Unicode 32-bit version, and it will install AutoHotkey v1.1.14.03. You will now be able to run an AHK script by double-clicking it and you will now see a Compile Script option when you right-click on an AHK file. Regards, Joe
Avatar of gci_ee

ASKER

Brilliant, that's worked a treat. Can't believe it was something so simple.

Many thanks again for the input.

Cheers
You're very welcome! I'm glad it's working for you now. Regards, Joe