Merge PDF files with related filenames automatically

12,240

(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

Share:
12,240

Related videos on Youtube

007MrBlackLight
Author by

007MrBlackLight

Updated on September 18, 2022

Comments

  • 007MrBlackLight
    007MrBlackLight over 1 year

    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
    
    • Paul
      Paul over 8 years
      Can you please add more example of the folder structure and other pdf that have to be merged separately?
    • 007MrBlackLight
      007MrBlackLight over 8 years
      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.
    • Paul
      Paul over 8 years
      Do not add new information in a tiny comment. Simply update your post.