I need to merge/combine several PDF files.
What I have is multiple PDF files in one folder. The PDFs that need to be clubbed/combined together would have a partial file name in common; for example:
123456_ABCD.pdf
123456_EFGH.pdf
123456_IJKL.pdf The file name would start with numbers then underscore then file name. I'm going to have hundreds of similar PDFs, and I'm looking for a way to automatically merge these files using a batch script, VBA in Excel, or whatever.
The output of the new file should be in a separate folder with the name 123456.pdf. I use a Windows OS. I have Adobe Reader, PDF995 and CutePDFwriter installed in my system.
I can manage to get all the pdf's in one main folder. That folder would have multiple pdf's like below.
123456_ABCD.pdf
123456_EFGH.pdf
123456_IJKL.pdf
111111_ABCD.pdf
111111_EFGH.pdf
222222_IJKL.pdf
222222_WXYZ.pdfIn this scenario the pdf's that I am expecting in the output folder are:
123456.pdf
111111.pdf
222222.pdf 3 1 Answer
(Untested)
- Download pdftk (direct link here).
- Extract it somewhere.
- Put pdftk.exe and the batch together.
- Then open
cmdwhere the batch is located* and run it.
* (shift+RightClic open command prompt here)
The script should works if the folder structure is like this:
|
+---111111
| 111111_ABCD.pdf
| 111111_EFGH.pdf
|
+---123456
| 123456_ABCD.pdf
| 123456_EFGH.pdf
| 123456_IJKL.pdf
|
\---222222 222222_IJKL.pdf 222222_WXYZ.pdfAnd not in a single folder where all the files are there.
@echo off
setlocal enabledelayedexpansion
rem source root folder where to crawl pdfs.
set "source=c:\mydoc\pdf"
rem destination folder
set "destination=c:\mydoc\merged"
for /f "delims=" %%a in ('dir /b /s /ad /o:n "%source%"') do ( set _pdffiles= for /f "delims=" %%i in ('dir /b /a-d /o:n "%%a\*.pdf"') do ( set _pdffiles=!_pdffiles! "%%i" set "_outputpdf=%%~ni" ) echo pdftk.exe !_pdffiles! cat output "%destination%\!_outputpdf:~0,6!.pdf"
)Remove echo if you think the batch is Okay.
Answer largely inspired by this