Page 1 of 1

Merge multiple Pdf files from sub-folders

Posted: 12 Oct 2014 05:38
by foncesa
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.

Re: Merge multiple Pdf files from sub-folders

Posted: 12 Oct 2014 06:10
by foxidrive
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)?

Re: Merge multiple Pdf files from sub-folders

Posted: 12 Oct 2014 08:17
by foncesa
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.

Re: Merge multiple Pdf files from sub-folders

Posted: 12 Oct 2014 08:19
by foxidrive
I don't know anything about pdftk.exe

Re: Merge multiple Pdf files from sub-folders

Posted: 12 Oct 2014 08:43
by foncesa
Hi,

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

Re: Merge multiple Pdf files from sub-folders

Posted: 12 Oct 2014 11:46
by Compo
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)

Re: Merge multiple Pdf files from sub-folders

Posted: 12 Oct 2014 12:37
by foncesa
Hi,

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