0

So I can use pdftk just fine (its a command line program to combine pdfs), however I want to come up with a way so that it combines the pages and always put the pdf ending in Summary as the first page.

My files are in the following format:

(Address) Summary.pdf
(Address) Top FG.pdf
(Address) Roof.pdf
(Address) Flashing.pdf

And there can be a few more, but it is always in this format but obviously the addresses always change. I just want it to put the page ending in Summary as the first page, the rest of the order I don't care about.

I know pdftk can combine like this:

pdftk (Address) Summary.pdf (Address) Top FG.pdf (Address) Roof.pdf (Address) Flashing.pdf cat output Combined.pdf

and this would work and put the summary page first, but how I can make a batch script to due this because obviously the address always changes and doing it manually would defeat the purpose. I have an idea in my head how it should work just haven't been able to get it to work.

  • 1
    Although I don't use `pdftk`, I am pretty sure that your line above should _not_ work. How `pdftk` identify each file? It starts at `(` and end at `.pdf`? IMHO each filename should be enclosed in quotes... Besides, although _"obviously the addresses always change"_ we have no idea of where these "addresses" names comes from. They are files in your disk? _All_ the files in one folder? How we can try to help you if you don't provide the details? – Aacini Aug 28 '18 at 00:55
  • Hey Sorry, the (Address) was just used to show that that is variable and will not be constant. So its a changing address that is different for each job. They are all files on the disk that I download from my email. They come as seperate pdfs that I want to combine into one single PDF with the first page being the Summary page, and yes all in the same folder. – Michael Johnson Aug 28 '18 at 01:37

1 Answers1

0

Ok. I usually ignore this type of questions (when OPs don't help us to solve their own problems)... However, I have a little time to spare now, so here we go!

First, the input:

C:\Users\Antonio\Documents\test> dir
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 0895-160E

 Directorio de C:\Users\Antonio\Documents\test

27/08/2018  09:38 p. m.    <DIR>          .
27/08/2018  09:38 p. m.    <DIR>          ..
27/08/2018  09:35 p. m.                20 (Address) Flashing.pdf
27/08/2018  09:35 p. m.                20 (Address) Roof.pdf
27/08/2018  09:35 p. m.                20 (Address) Summary.pdf
27/08/2018  09:35 p. m.                20 (Address) Top FG.pdf
27/08/2018  09:45 p. m.               274 test.bat
               5 archivos            354 bytes
               2 dirs  393,053,315,072 bytes libres

Note that the input files are exactly the same given in the question, and that the files are "all in the same folder" as specified by the OP.

Next, the Batch file:

@echo off
setlocal EnableDelayedExpansion

set "files="
for %%a in (*.pdf) do (
   set "name=%%~Na"
   if "!name:~-7!" equ "Summary" (
      set "files=%%a !files!"
   ) else (
      set "files=!files! %%a"
   )
)

ECHO pdftk !files! cat output Combined.pdf

Finally, the output:

pdftk (Address) Summary.pdf  (Address) Flashing.pdf (Address) Roof.pdf (Address) Top FG.pdf cat output Combined.pdf

Note that this output is exactly the same requested in the question. This means that the OP just need to remove the ECHO part in the last line in order to execute the wanted command! (and that I never will modify this code...)

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Hey Aacini, I thought I answered all the questions you had. Apologies if I had not. The addresses are obviously different addresses depending on the job. i.e. Lot 123 Fig Street, Emerald Hills, Lot 145 Raby Rd, Standrews. They are not static they are always changing, but they always end in either Summary, Top FG, Roof, or Flashing. All I wanted was a way to where I could just have the batch file look at the end to see the Summary part and put that as the first page when it combines all files. – Michael Johnson Aug 28 '18 at 04:55
  • I've tried running the code attached as you did with the same folder structure and everything but it comes back with errors saying it failed to open the PDF. It looks like its trying to open (Address) and then open Summary.PDF, not opening (Address) Summary.PDF. If you get what I mean. – Michael Johnson Aug 28 '18 at 05:02
  • 1
    @MichaelJohnson, because your filenames have spaces etc. you should enclose them with doublequotes, in order to prevent `PDFTk` from delimiting the list incorrectly. – Compo Aug 28 '18 at 07:15
  • I took the freedom to incorporate @Compos hint into the answer. –  Aug 28 '18 at 09:53
  • @MichaelJohnson: My code takes the _files on disk_ whichever names they have. I needed to create some files in order to test the code, so I asked: _"we have no idea of where these "addresses" names comes from"_. Your answer did not provide any new info (_"its a changing address that is different for each job"_), so I used the same example names you provided before... – Aacini Aug 28 '18 at 11:08
  • @LotPings: Excuse me, but such a hint was _not_ from Compo. In [my first comment](https://stackoverflow.com/questions/52048567/need-batch-script-to-merge-pdfs-with-pdftk-and-put-first-page-based-on-file-name?noredirect=1#comment91048148_52048567) I said: _"I am pretty sure that your line above should not work... IMHO each filename should be enclosed in quotes"_. The OP just ignored this point, so my code just honored the original example... **==>** Let's the OP to do some effort on his side to solve his problem, don't you think? – Aacini Aug 28 '18 at 11:12
  • It's your answer and I tried to help - not all newbies are firm enaugh in batch to know where to put the additional double quotes. Posting an answer you know is **not** or only indirectly helpful doesn't IMO apply to the [Code of Conduct](https://stackoverflow.com/conduct). –  Aug 28 '18 at 11:19
  • @LotPings: You know this type of questions are usually downvoted and closed. At first, I wanted the OP realize that his question was very incomplete, and I still want the same. If you solve all OP's problems in his first question he will be accustom to expect the same treatment the next time instead of make an effort to try to be clearer. – Aacini Aug 28 '18 at 11:36
  • Thanks, yeah sorry obviously I should have added the quotes even when I've used the program before I've surrounded them by quotes just a brain fart there. Now when I run it it does put them all on the same line but for some reason is still saying it can't find the file even though they are there. If I run the pdftk command by itself with the line the OP (obviously with quotes around each pdf) it runs fine so not sure what in the batch file is making it fail to find the files. – Michael Johnson Aug 28 '18 at 22:19
  • Well, it is very simple to know what exactly the Batch file is executing: just insert an `echo !files!` line! – Aacini Aug 29 '18 at 01:24
  • Thank you Aacini! That echo showed me that it was surrounding the entire filename for all the files in quotes rather than each one individually. All I had to do was change this line: `set "files=!files! %%a"` to `set "files=!files!" "%%a"` and it worked perfectly. :) Apologies for not providing good details previously and for missing where you said to surround by quotes. – Michael Johnson Aug 29 '18 at 01:42
  • @Aacini anyway to contact you regarding pdftk ? – Houston we have a problem Dec 17 '20 at 21:19
  • @Houstonwehaveaproblem: I know nothing about pdftk. Sorry... – Aacini Dec 22 '20 at 01:13