Help me automate a PDF-merge task!
July 13, 2015 1:44 PM   Subscribe

For work, I've been doing a task that involves taking all PDFs in a directory, merging them, and giving them a name based on the name of the folder. I'd like to automate this task.

I've been using PDFtk to merge files, and it's great when I feed it a list of specific files - but I have to individually add the name of every file in the folder, and write the name of the output file.

PDFtk has a command-line interface, and I've got some experience with batch files, but I'm not sure if this is a task that can be done with a batch file.
posted by LSK to Computers & Internet (6 answers total) 3 users marked this as a favorite
 
Best answer: from command line: pdftk *.pdf cat outfile filename.pdf

* selects all files in the folder
cat is the command to concatenate them (join them)
outfile uses the next filename to name the output file so in this case it would name it filename.pdf

note: it does this in alphabetical order in the folder.
posted by czytm at 1:52 PM on July 13, 2015


Best answer: Alternatively: pdftk *.pdf cat output ${PWD##*/}.pdf

${PWD##*/} will return the directory name so ${PWD##*/}.pdf will name the ouput file directory_name.pdf
posted by czytm at 1:57 PM on July 13, 2015


Response by poster: Thanks! Is there a way to go through every folder in a folder and do that? Basically, I've got one large folder with lots of folders in it, and I'd like to go through each of those folders with a single click if possible.
posted by LSK at 2:01 PM on July 13, 2015


my bash is a little rusty, but if the directory only has directories in it, try:
for D in `ls`; do pdftk ${D}/*.pdf cat output ${D}.pdf; done;
this will throw errors if there are other files in that top level folder.
posted by czytm at 2:55 PM on July 13, 2015


Response by poster: I forgot to mention - and this totally changes things, I realize - I'm doing this in Windows. The PDFtk syntax was correct and helpful, but I'm not going to be able to use any bash.
posted by LSK at 3:08 PM on July 13, 2015


cygwin, msys, VM, gitbash, etc??
posted by turkeyphant at 5:37 PM on July 13, 2015


« Older I want to know what food is. I want you to show me...   |   Where does the potential energy go? Newer »
This thread is closed to new comments.