Solved

batch to convert pdf to jpg command prompt

Posted on 2014-07-30
17
1,508 Views
Last Modified: 2014-07-31
hello I want to convert pdf to jpg in a batch and I'm using the:http://www.imagemagick.org/
 
   They way to use is simply:

 convert file.pdf file.jpg

Open in new window


   Now I want to do a batch in command prompt. I have a directory called c:\data inside that directory there are a lot of sub directories each directory by the same way inside has different pdf files with different names then I want that the batch look for all sub directories inside c:\data and then look inside each sub directory for the pdf files if found convert that pdf file inside the same directory where was found and exist the pdf found and with the same name than the original pdf and on that way with all sub directories and all pdf files. Of course some of that sub directories contain a lot of pdf files another sub directories have just one pdf or two and some has other kind of formats like flv, doc or whatever and that other formats different than pdf must not be touched must be converted the pdf files only. I tried to do this batch but doesn't work:

For /R C:\data %%I IN DO (.) convert "\%%I\*.pdf" "\%%I\*.jpg"

Open in new window

  Could anyone can help me in modify the script to make work and put the code?
Thank you so much
0
Comment
Question by:coerrace
  • 6
  • 6
  • 5
17 Comments
 
LVL 13

Assisted Solution

by:duncanb7
duncanb7 earned 250 total points
ID: 40230840
Try this using forfile, window commnad, search all pdf at c: drive, and the read junk.txt file without its extension and convert it into jpg. All jpg files will be located the directory where this batch file you run, you can change it to other directory if need.

Duncan

@echo off
forfile /p "c:" /s /m *.pdf" >junk.text
for /f "tokens=1 delims=." %%F in ('type junk.txt') do (convert %%F.pdf" %%F.jpg")
del junk.txt

Open in new window

0
 

Author Comment

by:coerrace
ID: 40230881
I don't want to put the destination files in in one directory if you read the main topic I need the files  converted in each sub directory by different for example:
 a directory called "one" and another called "two"
   inside "one" there are these pdf files: 1.pdf, 2.pdf and 3.pdf and the directory "two" has these pdf files other1.pdf and other2.pdf
   These 2 directories are inside c:\data and the batch must convert for example when pass to directory "one" convert the files 1.pdf, 2.pdf and 3.pdf to 1.jpg, 2.jpg and 3.jpg and store in the same "one" directory and when the script pass to directory "two" must convert the other1.pdf and other2.pdf to other1.jpg and other2.jpg and store inside same "two" directory not in other. If in "one and "two" directories there  are .flv or any other extension different than pdf the script must ignore or not convert or touch the file.
   This is an example but c:\data could have hundred of directories not just "one" and "two" and all the files inside each sub directory must respect the location and must be together where the original pdf file reside but never throw in one directory all the conversion.
   Any modification to do this in the main script I posted?
   Thank you
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40230884
you mean that search file and convert it to jpg file and put at the same location, right ?

Duncan
0
 

Author Comment

by:coerrace
ID: 40230901
well if the word search apply is search but for example when you use copy *.do  c:\ it grab just the .doc files to c:\ but maybe is not a kind of search but if apply the word is ok. In what I need is for each subdirectory convert only the pdf files and convert to jpg and store in same sub directory where pdf is and the rest of other extensions must be ignored and on that way for all sub directories inside c:\data
Thank you
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40230911
So just, you mean that search pdf files at c: drive  and convert it to jpg files and save them at the same location they are converted from.

Duncan
0
 

Author Comment

by:coerrace
ID: 40230923
not c: is c:\data and inside c:\data there are a lot of subdirectories the pdf "search" must be in all subdirectories inside c:\data for example search in c:\data\one in c:\data\two and so on there are hundred of sub directories there, "one" and "two" are not real could be any name in that hundred of directories inside c:\data
Thank you
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40230931
You want the mogrify command, not convert, because mogrify accepts a wildcard. This command should do it for you:

For /R C:\data %%I IN DO (.) mogrify -format jpg *.pdf

Open in new window


Regards, Joe
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40230932
try this code, do it in example files and directory first, it will work,

@echo off
forfiles /p "c:\data" /s /m *.pdf  /C "cmd /c echo @path ">junk.txt
for /f "tokens=1 delims=." %%F in ('type junk.txt') do (convert %%F.pdf" %%F.jpg")
del junk.txt

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40230938
I should have mentioned that <mogrify.exe> is in the same ImageMagick install folder as <convert.exe>, but you probably figured that out already. :)  However, the one-line code I posted is wrong. I'm very sorry about that — I feel really bad. Here's code that I just tested with mogrify and it works fine:

FOR /R "d:\0tempd\test\" %%I in (.) DO (
 Pushd %%I
 mogrify -format jpg *.pdf
 Popd )

Once again, I apologize for the previous bad code. Regards, Joe
0
 

Author Comment

by:coerrace
ID: 40230971
joe winograd I used your line and appears is bad formatted say:
do was unexpected at this time and like that if you make work sure go to search in subdirectories? Looks great I changed the IN DO (.) by IN (.) DO and does more but I see not go to inside sub directories to conver the files the error say:

mogrify: unable to open image `*.pdf': Invalid argument @ error/blob.c/OpenBlob/
2587.

   And that means is looking inside c:\data but not inside the subdirectories could you check I like mogrify.

duncanb7 why you use the del command? if it is just convert? don't want to destroy any data is a server.
0
 
LVL 51

Accepted Solution

by:
Joe Winograd, EE MVE earned 250 total points
ID: 40230976
Our messages just crossed. See the code in my last post. I tested it and it works fine. But I had my own test folder in there (d:\0tempd\test\). Of course, change it to yours (c:\data\) — so you want:
FOR /R "c:\data\" %%I in (.) DO (
 Pushd %%I
 mogrify -format jpg *.pdf
 Popd )

Open in new window

0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40231030
del command , just delete junk.txt temp file only after finishing all convert, that file
is not related to convert

I have checked the script is working I used "copy" to simulate "convert" effect

Duncan
0
 

Author Closing Comment

by:coerrace
ID: 40231596
Both scripts worked is just needed to run in my case in c:\ if I run from c:\temp for example not work but from c:\ all perfect.
Thank you
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40231714
> if I run from c:\temp for example not work

Hmmm, I don't understand that. I haven't tried Duncan's solution, but I can tell you that mine should work fine running from c:\temp (in my test, I used d:\0tempd\test and it worked fine). In any case, you're welcome — and I'm glad you got it working! Regards, Joe
0
 

Author Comment

by:coerrace
ID: 40231738
I opened this new question just for a modification if you can help there:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28487736.html

Thank you
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 40232090
I'm leaving my office now for around 3-4 hours. I'll look at your new question as soon as I return. Regards, Joe
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40232102
thx for your pt

ve a nice day

duncan
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now