Batch file to combine pdf files in alphabetical order

1

I want to create a batch file to combine selected pdf files in alphabetical order. Currently I have the following script:

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

The script file is saved as a .cmd file and a shortcut of it is placed into the 'SHELL:SENDTO' folder. Therefore it is possible to select some .pdf files, click on the right-mouse button and run the file to create binder.pdf which is a copy of the selected pdf files combined together.

The only problem left is that the batch file creates a pdf in random order when I select 15+ .pdf files. Is it possible to make sure that the selected .pdf files are merged in alphabetical order?

Ronald

Posted 2014-10-22T06:32:57.513

Reputation: 11

Answers

1

I'm sure there's a more straightforward way to do this, but here's my first attempt:

setlocal enabledelayedexpansion

:: Save all names to temporary file
if exist pdfs.txt del pdfs.txt
for %%a in (%*) do echo %%a >> pdfs.txt

:: Loop over sorted names
for /f "usebackq" %%a in (`type pdfs.txt ^| sort`) do (set command=!command! %%a)
pdftk.exe %command% cat output "%~dp1binder.pdf"

:: Clean up
del pdfs.txt

Berend

Posted 2014-10-22T06:32:57.513

Reputation: 1 824