Page 1 of 1

run a program with FOR command in batch, for some files

Posted: 25 Dec 2012 09:59
by taherkhani
Hi everyone,

i want run a program over some files with specific extension,

for example run a command-line program like pdftk-1.44-win over
all .pdf files in a folder for delete page 1 of all .pdf files.

for example, for rotate the first page in.pdf to 90 degrees clockwise and save output to out.pdf,
i should run this command:

pdftk in.pdf cat 1E 2-end output out.pdf

question is: how i can rotate first page of all .pdf files in a folder and save output with same

name in a different folder with this program pdftk?

if i know how write this program with FOR command, i can run any specific program
over any files with respect to that program.

thanks a lot in advance.

Re: run a program with FOR command in batch, for some files

Posted: 25 Dec 2012 10:42
by abc0502
This should work, but test it first:

Code: Select all

@Echo Off

set "source=D:\folder\containing\all\pdf\files"
set "destination=D:\folder\to\save\new\files\to"
set "pdftk=C:\location\of\pdftk.exe"

Setlocal EnableDelayedExpansion
For /F "delims=" %%A in ('DIR /B /A:-D "%source%\*.pdf"') Do (
    "%pdftk%" "%%~nA.pdf" cat 1E 2-end output "%destination%\%%~nA.pdf"
)

The idea is to get all the pdf file's names one by one and use the command, so this for loop will iterate trhow all pdf files one py one and the name of each file will be represented as "%%~nA" which mean the name of the file without the extension.

Remember to set the variables at the start of the batch