sreimer
sreimer

Reputation: 4998

Batch file - convert only new files

I have a growing directory of PDFs that for which I'm using ImageMagick to convert to JPGs. As it get's larger, it doesn't make sense to re-convert all the files each time.

Here's my command:

FOR /R %%a IN (*.pdf) DO magick -density 120 "%%~a" -quality 90 "%%~dpna.jpg"

How do I structure my batch file or ImageMagick command to only process PDFs that do not already have a match JPG?

Upvotes: 0

Views: 251

Answers (1)

Mofi
Mofi

Reputation: 49127

A simple solution is using command IF to check existence of *.jpg for current *.pdf file:

for /R %%I in (*.pdf) do if not exist "%%~dpnI.jpg" magick -density 120 "%%I" -quality 90 "%%~dpnI.jpg"

Another possibility is to use the archive attribute of files which is automatically set if a file is created or modified in a directory.

for /F "delims=" %%I in ('dir *.pdf /AA-D-H /B /S 2^>nul') do (
    %SystemRoot%\System32\attrib.exe -a "%%I"
    magick -density 120 "%%I" -quality 90 "%%~dpnI.jpg"
)

The main advantage of using this solution is that a PDF file once converted to a JPEG file is overwritten, the conversion to JPEG is done once again for updated PDF file.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • attrib /?
  • dir /?
  • for /?
  • if /?

Upvotes: 1

Related Questions