?
Solved

executing an exe for all files in a folder

Posted on 2010-11-27
14
Medium Priority
?
369 Views
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 !
0
Comment
Question by:hipal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 20

Expert Comment

by:edster9999
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

0
 

Author Comment

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

how to do that ? or will the above do
0
 
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.
0
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 

Author Comment

by:hipal
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
0
 
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 :

http://commandwindows.com/batchfiles-iterating.htm
0
 
LVL 20

Expert Comment

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

Author Comment

by:hipal
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'
0
 

Author Comment

by:hipal
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
0
 
LVL 38

Expert Comment

by:BillDL
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\"
pause

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\"
pause

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\"
pause

@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\"
pause

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.
0
 
LVL 20

Expert Comment

by:BuggyCoder
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
0
 
LVL 38

Accepted Solution

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

The software is "123 PDF to Image":
http://www.freepdftoimage.com/

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"

Where:

-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:
C:\WINDOWS\system32\convert.exe
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 ;-)

hipal

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.

Bill
@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.
echo There are already JPG images in the destination folder:
echo %DEST%
echo These will be overwritten if you proceed.
echo.
echo Move or Copy the files to another folder then run again.
echo.
echo Press any key to quit ...
pause > nul
goto :END 

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

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

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

Open in new window

0
 

Author Closing Comment

by:hipal
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 !!
0
 
LVL 38

Expert Comment

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

Author Comment

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

http://www.experts-exchange.com/Programming/Misc/Q_26642437.html

thanks again..

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

764 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