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?


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
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.