I am trying to create a batch file using PDFtk to burst combine file in a certain directory to the output folder using the source file name (which can vary) as the input file name. for example
source directory:- D:\Temp\IN destination directory:- D:\Temp\OUT File name:- abcdefgh.pdf (which can vary) desired output file name:- abcdefgh-001.pdf, abcdefgh-002.pdf and so on
My batch file will reside in D:\Script The PDFtk.exe is in D:\PDFtk Server\bin
I tried for 1 whole day I can't get the input filename for the output. Can anyone help
My existing program:-
CD D:\Temp\IN
for /f "tokens=*" %%A in ('dir /b D:\Temp\IN\*.pdf') do (
set prefix=%%~ni )
set outname=%prefix%-%%03d-00.pdf
path D:\PDFtk Server\bin
pdftk.exe D:\Temp\IN\%prefix% burst output D:\Temp\OUT\%outname%
exit
There are multiple errors in your approach the first one is that in a case like this you need local delayed expansion. For an initial %
later use it as !
.
CD D:\Temp\IN
This is ok but would be better if it allowed for /Drive shifting to "quoted drive folder" (beware input folder must NOT in this case be same as output folder with this approach, it is best if output is a sibling as you have done or use a common parent is better.)
CD /D "D:\Temp\IN"
for /f "tokens=*" %%A in ('dir /b D:\Temp\IN\*.pdf') do (
set prefix=%%~ni )
You have mixed up %%A and %%i they should be the same, tokens
is not needed in this case but usebackq is for odd filenames and add quotes, don't need current directory, and )
is way too early.
setlocal enabledelayedexpansion
for /f "usebackq delims==" %%A in (`dir /b *.pdf`) do (
set "prefix=%%~nA"
next part
set outname=%prefix%-%%03d-00.pdf
again best quoted and needs using expansionset "outname=.prefix!-%%03d-00.pdf"
path D:\PDFtk Server\bin
pdftk.exe D:\Temp\IN\%prefix% burst output D:\Temp\OUT\%outname%
better if combined and quoted you don't need to use Current Directory
"D:\PDFtk Server\bin\pdftk.exe" ".prefix:.pdf" burst output "D:\Temp\OUT\!outname!"
the terminal bracket goes here)
exit
is superfluous
so all in all
CD /D "D:\Temp\IN"
setlocal enabledelayedexpansion
for /f "usebackq delims==" %%A in (`dir /b *.pdf`) do (
set "prefix=%%~nA"
set "outname=!prefix!-%%03d-00.pdf"
"D:\PDFtk Server\bin\pdftk.exe" "!prefix!.pdf" burst output "D:\Temp\OUT\!outname!"
)
The whole command could be simplified to ONE line however to show where cuts can be made
try this
CD /D "D:\Temp"
for /f "usebackq delims==" %%A in (`dir /b IN\*.pdf`) do (
"D:\PDFtk Server\bin\pdftk.exe" "IN\%%~A" burst output "OUT\%%~nA-%%03d-00.pdf"
)
the need for usebackq delims==
is because of later problems with characters such as filename (1).pdf
so we cannot shorten much more than
CD /D "D:\Temp" & for /f "usebackq delims==" %%A in (`dir /b IN\*.pdf`) do ("D:\PDFtk Server\bin\pdftk.exe" "IN\%%~A" burst output "OUT\%%~nA-%%03d-00.pdf")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.