Merging PDFs

November 18, 2010 at 10:19:38
Specs: Windows 7
I am trying merge some pdfs using pdftk (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) from a folder with 1000's of pdfs. An example of pdf names:

1234_001.pdf
1234_002.pdf
1234_003.pdf
1235_001.pdf
1235_002.pdf

So I am trying to merge 1234_001, 1234_002 and 1234_003 together.

This is my code I have so far:

for /f "tokens=* delims= " %%a in ('dir/b') do (
set str=%%a
set str=%str:~0,8%
C:\Users\Alan\pdftk-1.44-win-install\bin\pdftk %str%*.pdf cat output C:\pdf\Finised\combined.pdf
del "C:\pdf\%str%.pdf"
)

Please let me know of your thoughts.


See More: Merging PDFs

Report •


#1
November 18, 2010 at 10:30:42
Please let me know of your thoughts.
Does it work?
If so, why tell us?
If not, why not tell us the problem?

How To Ask Questions The Smart Way


Report •

#2
November 18, 2010 at 11:16:30
Sorry for that.
I am having problems with the %%a and the str
this is a copy of the cmd prompt when I run the for loop one time

C:\pdf>for /F "tokens=* delims= " %a in ('dir/b') do (
set str=%a
set str=~0,8
C:\Users\Alan\pdftk-1.44-win-install\bin\pdftk *.pdf cat output C:\pdf\Finised\
combined.pdf
del "C:\pdf\*.pdf"
pause
)

C:\pdf>(
set str=00011702_001_!ext!.png
set str=~0,8
C:\Users\Alan\pdftk-1.44-win-install\bin\pdftk *.pdf cat output C:\pdf\Finised\
combined.pdf
del "C:\pdf\*.pdf"
pause
)

as you can see on the 3rd line the str is gone and all that is left is the "~0,8"

the pdftk and the del work great.

the problem is trying to set str=%str:~0,8%


Report •

#3
November 18, 2010 at 11:38:29
Normally, I'd tell you to use SETLOCAL ENABLEDELAYEDEXPANSION, but your files have exclamation marks in them, so that's out.

Your best bet would be to use a function (untested):

for %%a in (*) do call :doWork "%%a"
goto :EOF

:doWork
set str=%~1
set str=%str:~0,8%
C:\Users\Alan\pdftk-1.44-win-install\bin\pdftk %str%*.pdf cat output C:\pdf\Finised\combined.pdf
del "C:\pdf\%str%.pdf"

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
November 18, 2010 at 12:00:14
The files will not have exclamation marks, this is just a rare accurace.

It's starting to work a little better this is what I am getting in the cmd prompt now:

C:\pdf>call :doWork "00011701_002.pdf"

C:\pdf>set str=00011701_002.pdf

C:\pdf>set str=00011701

C:\pdf>C:\Users\Alan\pdftk-1.44-win-install\bin\pdftk 00011701*.pdf cat output C
:\pdf\Finised\combined.pdf
Error: Failed to open PDF file:
00011701*.pdf
Errors encountered. No output created.
Done. Input errors, so no output created.

C:\pdf>del "C:\pdf\00011701*.pdf"
Could Not Find C:\pdf\00011701*.pdf

C:\pdf>pause
Press any key to continue . . .


For some reason the wildcard * is not working


Report •

#5
November 18, 2010 at 12:19:47
I don't use pdftk, but a quick Google search indicates it's a POSIX program with a Windows port. POSIX doesn't implement wild cards the same way Windows does, so that may be your problem.

How To Ask Questions The Smart Way


Report •

#6
November 18, 2010 at 12:22:46
thank you for your quick responses!

Why won't the wildcard on the delete work?

C:\pdf>del "C:\pdf\00011701*.pdf"
Could Not Find C:\pdf\00011701*.pdf


Report •

#7
November 18, 2010 at 12:26:37
EDIT: Nevermind. I donno. Is the FOR loop pulling its list from somewhere that isn't C:\pdf?

How To Ask Questions The Smart Way


Report •

#8
November 18, 2010 at 12:30:03
Thank you for all your help!

if you don't mind me asking what is SETLOCAL ENABLEDELAYEDEXPANSION?


Report •

#9
November 18, 2010 at 12:41:35
It enables delayed expansion.
Instead of just using %var%, which is expanded before a loop runs, it allows the use of !var!, which is expanded while the loop runs.

Side note: As written, your script will throw file not found errors as part of its normal operation.

How To Ask Questions The Smart Way


Report •


Ask Question