Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


executing an exe for all files in a folder

Posted on 2010-11-27
Medium Priority
Last Modified: 2012-05-10
I want to run an executable for all files in a folder on windows 7
On command line it just takes one filename as input at a time.

What should I write in my batch file ?

this should be really simple and straightforward but not able to figure out. :(

Thanks !
Question by:hipal
  • 6
  • 3
  • 2
  • +2
LVL 20

Expert Comment

ID: 34222872
You would be looking at something like :

FOR %%f IN (*.*) DO command-goes-here %%f

You could change the *.* to match the files for example *.DAT


Author Comment

ID: 34222924
actually I want to give individual files as parameters to my program ..

how to do that ? or will the above do
LVL 13

Expert Comment

by:Naman Goel
ID: 34222928
use :  for /r <folder path> %%i in (*) do <your exe name> %%i

for example :

for /r c:\test %%i in (*) do start %%i

will open all files in c:\test.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 34222958
thanks for reply.

sorry bt I am new to this .. can you please explain this a bit .. as in what is  %%i  and /r
LVL 13

Expert Comment

by:Naman Goel
ID: 34222973
/R        Causes the command to be executed recursively through the sub-directories of an indicated parent directory

%%i is simple variable

this is just like a simple foreach loop in c#

same as following c# foreach loop
foreach(int i in intcollection) { //use i }

for information refer to :

LVL 20

Expert Comment

ID: 34222977
can you share the command that you use with one file name specified.

Author Comment

ID: 34223214
I have this convert.exe which takes as input a pdf filename and converts it into JPEG

and I want to make a batch so that it runs convert.exe on all pdf files in a folder ..let us say 'myPDFs'

Author Comment

ID: 34223427
I have tried  this

for /f %i in "C:\Users\A\Desktop\abc" DO C:\Program Files (x86)\123 PDF to Image\PDFtoImage" -i  %i -o "C:\Users\A\Desktop\abc\"

not working plz help
LVL 39

Expert Comment

ID: 34223997
If you used that command in a batch file (*.cmd or *.bat) then you should have doubled up the % symbol. Use single % symbols for the variables when run straight from a command window, and %% if used in a batch file.

Let me break down the FOR command so you know how it needs to be structured, and perhaps you can also type in   FOR /?   in a command window to see the usage.

FOR [/switch] ["options"] %var IN (set) DO command [params]

The (set) can be:
A file name, eg. (Filename.txt)
A command surrounded by single quotes, eg. ('type filename.txt ^| find "word"')
A text string surrounded by double quotes, eg. ("string of text")
A file set, eg. (*.txt)
A "start,increment_by,counter" sequence, eg. (1,1,10) (start at 1, increment by 1, and do it 10 times)

You missed out that all important (set) in your command.

Whatever is in those brackets is done first, so if you had ('dir *.txt') that would generate a directory listing. If you had (filename.txt) it would reading the first line of the named file, or if you had ("this is a text string") that would be used as the source data for the rest of the command.

The part before the (set) in the command reads whatever is generated by or contained in the (set) and modifies it if told to.  The part following the (set) does something with the resultant data, such as executing a command using the data as a parameter.

Most commonly the (set) will be a list of files or multiple lines of text which will be read and stored one line at a time.  The FOR command is a loop.  Each line would be stored in the variable with each pass, overwriting the last. So line1 would be stored in %i with the first pass, then line2 would replace line1 in %i with the 2nd pass, etc.

In your case you want to get a listing of all files in a named folder.  This could be achieved with a simple (*.*) or a ('dir /on /b /s "*.*"')

Taking naman_goel's example and putting it into a batch file with the paths you have provided would give you something like this that should work:

@echo off
cd "C:\Users\A\Desktop\abc"
for /r %%i in (*.pdf) do start /wait /b "C:\Program Files ^(x86^)\123 PDF to Image\PDFtoImage\convert.exe" -i  "%%i" -o "C:\Users\A\Desktop\abc\"

It changes directory to the folder containing the PDF files and walks through it  storing each filename as it gets to it in %%1 and executes the convert command on each file.

There are various ways that would do much the same thing:

@echo off
for /f %%i in ('dir /on /b /s "C:\Users\A\Desktop\abc\*.pdf"') do start /wait /b "C:\Program Files (x86)\123 PDF to Image\PDFtoImage\convert.exe" -i  "%%i" -o "C:\Users\A\Desktop\abc\"

That creates a directory listing of PDF files in the specified folder and reads off the first fully qualified path to each PDF file, stores it as %%i and then executes the convert command on it. The 2nd line is then read off and processed, etc, until it gets to the end of the file listing.

Note: I just typed the commands off the top of my head and have not tested them.  If you wish to run the batch file and SEE what commands WOULD BE passed to convert.exe, then add in    @echo    in front of the convert.exe command and it will output each command to screen so you can check first, eg.

@echo off
cd "C:\Users\A\Desktop\abc"
for /r %%i in (*.pdf) do @echo start /wait /b "C:\Program Files (x86)\123 PDF to Image\PDFtoImage\convert.exe" -i  "%%i" -o "C:\Users\A\Desktop\abc\"

@echo off
for /f %%i in ('dir /on /b /s "C:\Users\A\Desktop\abc\*.pdf"') do @echo start /wait /b "C:\Program Files (x86)\123 PDF to Image\PDFtoImage\convert.exe" -i  "%%i" -o "C:\Users\A\Desktop\abc\"

The pause on the last line of each is to keep the command window open for testing, because it would normally close after processing, even if the command failed.
LVL 20

Expert Comment

ID: 34224026
there is no big deal in this
convert *.pdf *.jpeg

just execute this in folder where your files are.
for example if i have to rename all the pdf files in a folder named myfiles to jpeg i will do it this way

c:\myfiles>ren *.pdf *.jpeg
LVL 39

Accepted Solution

BillDL earned 2000 total points
ID: 34224448
Presumably you tested your simple command, Buggy Coder?

The software is "123 PDF to Image":

It accepts the following command line parameters:

Example Quiet pdf to image conversion:
PDFtoImage.exe -i[l] "filename1" filename2 "filename3" [-imgtype X] [-dpi Y] [-quality Z] -o "outputfolder"


-i = input files
l = create log file in specified output folder

-imgtype = output image type (Default is 0):
   0 - bmp
   1 - jpg
   2 - wmf
   3 - emf
   4 - eps
   5 - png
   6 - gif
   7 - tiff
-dpi = DPI (dots per inch) settings. Default is 72
-quality = Image Quality of JPG/JPEG image. Default is 100.
-o Output folder (full path)
Additional explanations:
Filename1, etc. should be specified with the full path.
Filenames with spaces must be enclosed with double quotes.

Parameters -i and -o along with their values are mandatory
l, -imgtype, -dpi, -quality are optional.
You cannot have DPI or Quality parameter without Imgtype, but you can use Imgtype alone.

The program executable is:
C:\Program Files\123 PDF to Image\PDFtoImage.exe
(or in hipal's case C:\Program Files (x86)\123 PDF to Image\PDFtoImage.exe)
It does not add itself as a System or User environment variable, so running "convert" on a folder of PDFs won't work.  Easy enough to add its path to the environment variable I suppose, but unnecessary if you just use the path in the command.

Of MORE importance, however, is that the simplistic command:
convert *.pdf *.jpeg
would actually call the Windows FAT32 to NTFS conversion program:
on Windows XP and probably other recent Windows operating systems.

OK, so it wouldn't run and cause any damage with the incorrect parameters specified, but that's beside the point.  Your suggestion was reckless and demonstrates that you didn't consider that there may be another "convert.exe". Actually, hipal was partly to blame ;-)


Double check the path to your program. You misled us with your statement:
"I have this convert.exe which takes as input a pdf filename and converts it into JPEG",
and the command that you tried out.

I assume now, having inspected the program, that you mean:
"I have this convert  exe  which takes as input a pdf filename and converts it into JPEG",
and were actually referring to:
C:\Program Files (x86)\123 PDF to Image\PDFtoImage.exe

A have attached a batch file that works. Copy and paste it into a new *.CMD file in any folder.
All you need to do is change the paths against each of the   set=   lines at the top.

It will create a new sub-folder of the name that you specify against the    set DEST=   line if one does not already exist.  I put in the safety check to look for existing JPG images in the destination folder, because it overwrites existing ones when run.  That part can be stripped out if you wish.  Those    set=   lines just mean that you can use the variable names of %SRC% and %DEST% in the batch file and they will be populated with the paths as the batch file is run.

The for /f "tokens=* delims=" part of the command ensures that files and folder paths with spaces are stored properly. Without that it stops at the first space it finds and uses the part up to the space as the output file name.

The %%~nxi variable that is used to provide screen feedback just strips the fully qualified path to the PDF file back to filename and extension only.

Note: That trailing backslash in the FOR statement:
"%PROG%" -il "%%i" -imgtype 1 -o "%DEST%\"
is required.  It doesn't work properly if you don't add it.  This is a program requirement, not that of the batch file.

I assume you already know that multi-page PDFs will be converted into as many images as there are pages.

If you want any changes, just ask.  Hope it does what you want.

@echo off

set PROG=C:\Program Files (x86)\123 PDF to Image\PDFtoImage.exe
set SRC=C:\Users\A\Desktop\abc
set DEST=C:\Users\A\Desktop\abc\JPGs

if not exist "%DEST%" md "%DEST%"

if exist "%DEST%\output.log" echo. >> "%DEST%\output.log"

if not exist "%DEST%\*.jpg" goto :CONVERT
echo There are already JPG images in the destination folder:
echo %DEST%
echo These will be overwritten if you proceed.
echo Move or Copy the files to another folder then run again.
echo Press any key to quit ...
pause > nul
goto :END 

echo Processing PDF files in folder:
echo %SRC% ...

for /f "tokens=* delims=" %%i in ('dir /on /b /s "%SRC%\*.pdf"') do (
	echo Converting: %%~nxi 
	"%PROG%" -il "%%i" -imgtype 1 -o "%DEST%\"

echo PDFs converted to JPG images in folder:
echo %DEST%
echo Press any key to close window ...
pause > nul

Open in new window


Author Closing Comment

ID: 34224491
thanks a ton for such an elaborated reply..

I have no words to express my thanks .. as you spent so much a time on this ..

thanks again !!
LVL 39

Expert Comment

ID: 34225029
You are very welcome hipal. Glad to be of assistance.

Author Comment

ID: 34225258
Hi Bill .. please have a look at this


thanks again..


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Make the most of your online learning experience.
Introduction to Processes

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question