Merge multiple Pdf files from sub-folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
foncesa
Posts: 42
Joined: 12 Sep 2013 00:09

Merge multiple Pdf files from sub-folders

#1 Post by foncesa » 12 Oct 2014 05:38

Hi,

I have one pdf file in sub-folder of
C:\database\local\download\PDF\folder1\*.pdf
C:\database\local\download\PDF\folder2\*.pdf
C:\database\local\download\PDF\folder3\*.pdf
I am trying to merge this each pdf file from those sub-folders and send it to C:\ConsPDF\ folder with pdftk.exe

Code: Select all

@echo off
for /d %%a in ("C:\database\local\download\PDF\*") do (
   pushd "%%~a"
      "C:\Program Files (x86)\PDFtk\bin\pdftk.exe" *.pdf cat output "C:\ConsPDF\merge.pdf.bin"
       ren "C:\ConsPDF\merge.pdf.bin" "merged.pdf"
   popd
)

Its not creating a merged pdf file, any one please rectify the mistake.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merge multiple Pdf files from sub-folders

#2 Post by foxidrive » 12 Oct 2014 06:10

Without knowing the syntax of pdftk.exe your loop isn't checking for PDF files, it's checking for folders,
and the pdftk.exe command is not using each file in turn (assuming you fix the first problem) but it is expecting all the files to be in one folder - which they are not.

Can pdftk.exe output different pdf files into the same merged output file, three times (or more)?

foncesa
Posts: 42
Joined: 12 Sep 2013 00:09

Re: Merge multiple Pdf files from sub-folders

#3 Post by foncesa » 12 Oct 2014 08:17

Hi,
Foxidrive, that's true its checking inside the folder, instead of searching all the sub-folder for pdf file. Please provide some code for that and pdftk.exe it is capable for merging any number of files.
Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merge multiple Pdf files from sub-folders

#4 Post by foxidrive » 12 Oct 2014 08:19

I don't know anything about pdftk.exe

foncesa
Posts: 42
Joined: 12 Sep 2013 00:09

Re: Merge multiple Pdf files from sub-folders

#5 Post by foncesa » 12 Oct 2014 08:43

Hi,

I have taken this idea from your reply in this forum
http://www.dostips.com/forum/viewtopic.php?f=3&t=5220

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Merge multiple Pdf files from sub-folders

#6 Post by Compo » 12 Oct 2014 11:46

Instead of using wildcards, and if there aren't too many files involved, you may get away with just listing them.

Code: Select all

@Echo Off & SetLocal EnableExtensions
Rem Setting Environment
Set "PDFParent=C:\database\local\download\PDF"
Set "PDFMerged=C:\ConsPDF\merged.pdf"
Set "PDFtkProg=C:\Program Files (x86)\PDFtk\bin\pdftk.exe"
Set "PDFListed="

Rem Listing PDFs for catenation
PushD %PDFParent%||GoTo :EOF
For /D %%A In (*) Do (For %%B In ("%%A\*.pdf") Do (If Not Defined PDFListed (
   Set PDFListed="%%~B") Else (Call Set PDFListed=%%PDFListed%% "%%~B")))

Rem Catenating PDFs
If Defined PDFListed (
   For %%A In ("%PDFMerged%") Do If Not Exist "%%~dpA" (MD "%%~dpA"||GoTo :EOF)
   "%PDFtkProg%" %PDFListed% cat output "%PDFMerged%"
   Pause)

foncesa
Posts: 42
Joined: 12 Sep 2013 00:09

Re: Merge multiple Pdf files from sub-folders

#7 Post by foncesa » 12 Oct 2014 12:37

Hi,

Thanks Compo, It's perfect.
Thanks a lot.

Post Reply