Search code examples
batch-filepdftk

Merging .pdf files with Pdftk


I have many .pdf files on a folder. Also i have a .bat script that works together with Pdftk program. But when i execute the .bat it isn't working.

Follow my script:

@echo off
setlocal enabledelayedexpansion
FOR %%A IN (%*) DO (set command=!command! %%A)
pdftk.exe %command% cat output "%~dp1binder.pdf"

My .pdf files, .bat script and the pdftk.exe with it libiconv2.dll are all in the same folder.

Can someone help me? I need merge all the .pdf files in the folder.

Thank you!


Guys, this simple command are working fine for me, with only one problem. If don't have the file that is in the command, it won't process. Look the script:

@echo off
pdftk fbw1.pdf fbw2.pdf fbw3.pdf fbw4.pdf fbw5.pdf fbw6.pdf fbw7.pdf fbw8.pdf fbw9.pdf fbw10.pdf fbw11.pdf fbw12.pdf fbw13.pdf fbw14.pdf fbw15.pdf fbw16.pdf fbw17.pdf fbw18.pdf fbw19.pdf fbw20.pdf fbw21.pdf fbw22.pdf fbw23.pdf fbw24.pdf fbw25.pdf fbw26.pdf fbw27.pdf fbw28.pdf cat output testieee.pdf >nul 2>nul

For example: If i don't have the file fbw1.pdf into the directory, it won't process anything. I need that the program process all the files even are missing one or more...

Someone to help?

Thanks!


Ok, the command pdftk *.pdf cat output combined.pdf worked now, but it doesn't merge the .pdf files in sequence. Ex.: 1.pdf 2.pdf 3.pdf....is the sequence i want, but the command merge in this way: 1.pdf 3.pdf 2.pdf 7.pdf.... There's a way to recognize the sequence?

Thanks


Solution

  • EDIT New approach.

    • If you drag'n drop file(s) or a folder to the batch or pass at least one file/folder
    • the following batch will change to the referenced folder and
    • processes all pdf files in that folder combining them into binder.pdf
    • an eventually existing binder.pdf is renamed to binder.bak.pdf

    :: Q:\Test\2018\06\06\SO_50728273.cmd
    @echo off
    setlocal enabledelayedexpansion
    if "%~1" neq "" (
      Echo %~a1|findstr "d" 2>&1>Nul && Pushd "%~f1" || Pushd "%~dp1"
    ) else (
      Echo No arguments, need a path& pause & goto :Eof
    )
    Del /f binder.bak.pdf 2>&1>Nul
    if exist binder.pdf Ren binder.pdf binder.bak.pdf
    pdftk.exe *.pdf cat output binder.pdf
    PopD
    

    Without knowing what arguments you pass to the batch diagnosing is impossible.%* is replaced with all arguments you pass, the location of the output is determined by the path of the first argument %~dp1

    I ran your batch on my ramdisk a:

    Dir before:

    > dir A:\
     Verzeichnis von A:\
    
    2018-06-06  21:57            65.381 SO_5072812.pdf
    2018-06-06  21:56               163 SO_50728273.cmd
    2018-06-06  21:55            60.649 SO_50728273.pdf
                   3 Datei(en),        126.193 Bytes
                   0 Verzeichnis(se),  1.049.452.544 Bytes frei
    

    And after (I named the batch SO_50728273.cmd):

    > SO_50728273.cmd a:\*.pdf
    
    > dir
     Verzeichnis von A:\
    
    2018-06-06  21:58           125.756 binder.pdf
    2018-06-06  21:57            65.381 SO_5072812.pdf
    2018-06-06  21:56               163 SO_50728273.cmd
    2018-06-06  21:55            60.649 SO_50728273.pdf
                   4 Datei(en),        251.949 Bytes
                   0 Verzeichnis(se),  1.049.260.032 Bytes frei