1

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.pdf

In this scenario the pdf's that I am expecting in the output folder are:

123456.pdf
111111.pdf
222222.pdf
  • Can you please add more example of the folder structure and other pdf that have to be merged separately? – Paul Sep 26 '15 at 18:46
  • Thank you @Paul. 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.pdf In this scenario the pdf's that I am expecting in the output folder is 123456.pdf 111111.pdf 222222.pdf. I am not sure if this would help but the file name would start with numbers then underscore then file name. Please let me know if you need any more information. – 007MrBlackLight Sep 27 '15 at 21:51
  • Do not add new information in a tiny comment. Simply update your post. – Paul Sep 27 '15 at 21:58
1

(Untested)

  1. Download pdftk (direct link here).
  2. Extract it somewhere.
  3. Put pdftk.exe and the batch together.
  4. Then open cmd where 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.pdf

And 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

Update: There are two answers that complement my post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.