0

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?

sreimer
  • 4,913
  • 2
  • 33
  • 43

1 Answers1

1

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 /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    Technically you should check that the PDF is more recent than the JPG. Which is exactly what a `make` command would do, given a rule to convert PDF to JPG. – xenoid Jul 27 '18 at 19:28
  • @xenoid I don't recommend comparing file dates because of at least two reasons for this task. One reason is that this is quite difficult and error prune using pure batch commands, see [Find out if file is older than 4 hours in Batch file](https://stackoverflow.com/a/32670346/3074564). And what about the use case that a PDF was copied into a folder, converted to a JPEG file, later was found by the user that an older version of the PDF file was better, and so the user copies older version over newer version of PDF file? The JPEG file should be recreated from older, but better PDF file. – Mofi Jul 28 '18 at 11:28
  • @xenoid The use case with older version of a file should be processed although an output file is newer as created before from a newer version of input file is a general issue on using `make`. I am quite sure that every C/C++/C# programmer working in a team using a version control system runs again and again from time to time into this use case and have to start either a complete rebuild taking perhaps several minutes to finish or manually delete one or more object files and build the project to get older version of source file(s) compiled after a get/check out from version control system. – Mofi Jul 28 '18 at 11:39
  • This is by design of the source control system. If the file has changed locally, even because you pulled an old version, you have to recompile it, so the SCS gives it the time of pull. When I was doing C++, a full recompile could take hours... and you only need to update one strategic header file for this... – xenoid Jul 28 '18 at 12:39
  • @xenoid Yes, it is possible in most version control systems to configure which last modification date a file should have after get/checkout/pull or whatever is the term for the action in VCS to get a file from version system and store it locally: current date to get a compile of the file on next build or real last modification date of the file as stored in VCS. We have decided in our team to use real last modification date and not current date as we want to see on file date when it's content was last modified and not when the file was last time "downloaded" from version control system. – Mofi Jul 28 '18 at 12:49
  • @xenoid If `make` __on Windows__ would work like the second batch file, the last modification date of the file would not matter for build process. But I think we should stop this discussion here as it is off-topic according to question. You can post a solution using last modification file date if you think what I posted is not good for the task briefly described by [sreimer](https://stackoverflow.com/users/490183/sreimer). I would even upvote it if this alternate solution works fine as long as file dates do not change due to a daylight saving time or time zone change. – Mofi Jul 28 '18 at 12:55