Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have several pdf files and I would like to merge these files using pdftk. I can do it like this

C:\pdftk *.pdf cat output merged.pdf

This merges files using their alphabetical order, but I would like to order files using their creation date (timestamp). How is this possible? I'm using Win XP.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I think the best way is adding dates to file names. You can do it with Bulk Rename Utility, it's a very powerful utility and free.

Or, you can use another software to merge/split pdf files with gui, like PDFsam.

share|improve this answer

You can create a list of your PDF files that is ordered by creation date:

 dir /b /tc /od *.pdf > my-pdf-filelist.txt

(If you want the list -- instead of sorted by creation time -- the sorting happing by access time use '/ta' instead of '/tc'; if you want ordering by modification time, use '/tw'.)

Then use this list to create another temporary listfile, one that contains the timestamp and the PDF filenames with added dates + times on the same line:

 for /f "usebackq" %i in (my-pdf-filelist.txt) ^
        do (echo. %~ti %~nxi >> my-pdf-filelist.2)

Now check if your filelist my-pdf-filelist.2 does contain lines looking like this:

05/27/2009 06:08 AM fontproblems-in-footer-16_9_1557.pdf
01/20/2010 09:22 AM trainschedule-hannover.pdf
06/05/2010 07:30 PM Figure_001-a.pdf

Depending on the setting for your language+locale, your timestamp format may be different, and you may need to adapt the following step. In my case, I need to take care of the "AM/PM" thingie as well as the datestring in order to make sure the filenames will later really be in an alphabetic order that is the same as their timestamp order.

Last, use this temporary listfile to rename your original filenames so that they contain their timestamps as a prefix to their original names. But first test the intended command like this:

 for /f "usebackq tokens=1,2,3,4,5,6,7 delims=/: " %i in (my-pdf-filelist.2) ^
        do (echo.  "%o"  will be renamed to:  "%k-%j-%i-%n-%l-%m-%o")

Carefully check if this would work as intended. Finally, do the real renaming:

 for /f "usebackq tokens=1,2,3,4,5,6,7 delims=/: " %i in (my-pdf-filelist.2) ^
        do (ren "%o"  "%k-%j-%i-%n-%l-%m-%o")

Now that your PDFs are named in a way that makes 'alphabetic order' == 'timestamp order' you can simply run pdftk.exe:

 pdftk.exe *.pdf cat output merged.pdf
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.