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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taherkhani
Posts: 24
Joined: 07 Dec 2012 04:33

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

#1 Post by taherkhani » 25 Dec 2012 09:59

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

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

#2 Post by abc0502 » 25 Dec 2012 10:42

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

Post Reply