2

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

  • 1
    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` –  Jun 06 '18 at 19:52
  • 2
    Are you dragging and dropping a bunch of files onto your batch file? – Squashman Jun 06 '18 at 20:04
  • Yes, is it....im dropping into the batch. Theres a way to the .bat 'pick' the .pdf files into the folder and merge? –  Jun 06 '18 at 20:06
  • 2
    There is a limit to how long a command can be. Roughly 8191 bytes. If you drag and drop a bunch of files onto a batch file, it places the entire path to the file as the argument plus quotes for file paths with spaces. Depending on how many files your dropped you will have exceeded the maximum command length limit. – Squashman Jun 06 '18 at 20:09
  • 2
    If you would have read the help file for `PDFTK` you would have seen this example: `pdftk *.pdf cat output combined.pdf`. In other words, if all your files you need to combine are in the working directory, you can use a wildcard to reference all of them. – Squashman Jun 06 '18 at 20:12
  • 2
    There is absolutely no need to use a `FOR` command to loop through all of the arguments to assign them to an environmental variable. As your code already shows, you can access all the command line arguments with a wildcard, so why didn't you attempt this first: `pdftk.exe %* cat output "%~dp1binder.pdf"` – Squashman Jun 06 '18 at 20:15
  • I want the .bat find all the .pdf files in the directory and marge then automatically. –  Jun 06 '18 at 20:35
  • And what didn't you understand about my previous comment about this code: `pdftk *.pdf cat output combined.pdf`? – Squashman Jun 06 '18 at 21:00
  • @Squashman , i put this code before, but it only open the pdftk program, nothing more... –  Jun 06 '18 at 21:17
  • @Squashman take a look on my last edit! –  Jun 06 '18 at 21:45
  • I find it hard to believe that this does not work: `pdftk *.pdf cat output combined.pdf`. LotPings inferred that he tested it and it did work. – Squashman Jun 06 '18 at 22:11
  • 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 –  Jun 07 '18 at 13:59
  • @LotPings Take a look on my last edit ! –  Jun 07 '18 at 14:03
  • That sounds like an issue with the program itself. By default the Windows cmd prompt will list files in this order, 1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9. So what you are describing sounds like a problem with the way the program accumulates all files. You would think they accumulate in some kind of order but it doesn't sound like it does. – Squashman Jun 07 '18 at 14:15
  • I got it! Just putting 2 decimal after the word, like 'test001.pdf' 'test002.pdf', and it get the sequence. –  Jun 07 '18 at 16:05
  • I highly doubt your initial example of the files combining as 1, 3, 2 and 7 was accurate if they combine together correctly by zero filling them. All things being equal if the files are all named the same with a number at the end they would have initially combined in the order I previously stated. – Squashman Jun 07 '18 at 20:53
  • Can you help me on my another topic? Find on my profile. Thanks. –  Jun 07 '18 at 20:56

2 Answers2

1

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
  • the .bat needs find all .pdf files in the directory and merge then automatically... –  Jun 06 '18 at 20:37
  • The problem with using all pdf in the same folder is that binder.pdf will be included on successive runs. –  Jun 06 '18 at 20:55
  • 2
    @LotPings, I don't have PDFTK to test with but on their website they literally show this as an example: `pdftk *.pdf cat output combined.pdf`. So I am wondering if the program is aware of the PDF it is creating and does not include it? – Squashman Jun 06 '18 at 21:03
  • @Squashman Good point, just tested and you are right the output file is automatically excluded. –  Jun 06 '18 at 21:37
  • @Squashman I have to correct myself: errormsg `Error: The given output filename: binder.pdf matches an input filename. Exiting.` –  Jun 06 '18 at 22:16
  • @LotPings, so a combine with a non matching extension for the output file name and then rename the output file name back to .PDF. – Squashman Jun 06 '18 at 22:19
  • @Squashman See edited answer which should catch all the issues. –  Jun 06 '18 at 22:20
  • 1
    @LotPings, so if the output file is already an existing file in the folder it will give you that error but if you make sure you delete it first everything is fine? – Squashman Jun 06 '18 at 22:23
  • @Squashman Exactly, changed the batch to keep the previous version as binder.bak.pdf –  Jun 06 '18 at 22:43
  • I don't like the idea but as either the underlying file system or pdftk itself won't order the files properly - I suggest using the gui [ptdtk builder](http://www.angusj.com/pdftkb/) which has a sorting feature with the join option. –  Jun 07 '18 at 16:05
1

I have the same problem, and I am here. I think I have a better idea to do it. It may help you or the people will come here in future.

for my case,

ls *.pdf > list    
for i in `seq 1 340`;do grep "\-$i\-\-" list >> tmp; done
awk '{printf("%s ", $0)}' tmp > oneline
pdftk `cat oneline` cat output test.pdf

for your case

step 1 put all the file names into a file, like namelist.

ls *.pdf > namelist

step 2 sort the name list, suggest there are 400 files.

for i in `seq 1 400`;do grep $i namelist >> sortedNameList;done

step 3 re-organize the sortedNameList.

awk '{printf("%s ", $0)}' sortedNameList > namesInOneline

step 4 merge all the files into a single pdf file

pdftk `cat namesInOneline` cat output final.pdf
user1532868
  • 25
  • 2
  • 9