Solved

batch to convert pdf to jpg command prompt

Posted on 2014-07-30
17
1,923 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
[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
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 54

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
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
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
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 54

Accepted Solution

by:
Joe Winograd, EE MVE 2015&2016 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 54

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
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 54

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

628 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