Combining JPGS in folders and subfolders into PDFs

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
keljnr
Posts: 11
Joined: 2018-10-16T03:29:47-07:00
Authentication code: 1152

Combining JPGS in folders and subfolders into PDFs

Post by keljnr »

I have a script that, when I right click on a folder, combines all jpgs inside the folder into a PDF and renames the PDF to the name of the folder it resides in.

Code: Select all

cd %~dpnx1
for %%a in (.) do set currentfolder=%%~na
start cmd /k magick "*.{png,jpg,tif}" "%currentfolder%.pdf"
However, I have quite a lot of folders and currently have to do this one by one.
How can I create a function where I can right click on a folder, which searches subfolders and combines the jpgs to PDF?
So in the example below, Im wanting to create 3 PDFS (Folder A, Folder B and Folder C) by right clicking and running batch on the parent folder.

Example:
- Parent folder (one that I would right click and run script from)
|- Folder A
||- test1.jpg
||- test2.jpg
||- test3.jpg
|- Folder B
||- example1.jpg
|| - example2.jpg
|- Folder C
||- temp.jpg
||- temp2.jpg

I have attempted myself with the following, but no luck:

Code: Select all

cd %~dpnx1
for %%a in (.) do set currentfolder=%%~na
FOR /R %%a in (*.jpg) DO (
   magick "%%a" "%currentfolder%.pdf"
)
Hope you can help. Thank you.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining JPGS in folders and subfolders into PDFs

Post by snibgo »

What version of IM, on what platform? I assume Windows.

For your second script, you set currentfolder but then walk through the subdirectories. I assume you don't want that. Instead, you could loop through all the sudirectories. Within that loop, pushd to the directory, set currentfolder, loop through all of the files, then popd.
snibgo's IM pages: im.snibgo.com
keljnr
Posts: 11
Joined: 2018-10-16T03:29:47-07:00
Authentication code: 1152

Re: Combining JPGS in folders and subfolders into PDFs

Post by keljnr »

Thanks snibgo, I have set the following but my logic must be incorrect:

Code: Select all

cd %~dpnx1
FOR /R %%a in (.) DO (
   PUSHD
   set currentfolder=%%~na
   FOR /R %%a in (*.jpg) DO (
      magick "%%a" "%currentfolder%.pdf"
   )
   POPD
)

It looks like its looping through the directories but is just overwriting the same pdf in the root.
Its also not combining the jpgs into the pdf, just converting one jpg at a time. PDF also doesn't take the parent folders name.

What is it I've done incorrectly here?

Windows - Imagemagick version 7.08

Many thank.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining JPGS in folders and subfolders into PDFs

Post by snibgo »

You should pushd to a directory, so that line should be:

Code: Select all

PUSHD %%a
But then you loop through the jpg files, converting each one individually to the same PDF. The code in your first post seemed to be what you actually want, reading all the jpg files in a single magick command to make a single PDF.
snibgo's IM pages: im.snibgo.com
keljnr
Posts: 11
Joined: 2018-10-16T03:29:47-07:00
Authentication code: 1152

Re: Combining JPGS in folders and subfolders into PDFs

Post by keljnr »

Awesome, thanks snibgo...
It loops through, and when I give the pdf a set name, works perfectly:

Code: Select all

magick "*.{png,jpg,tif}" "test.pdf")
But still unable to get the pdf to rename to the folder name:

Code: Select all

cd %~dpnx1
FOR /R %%a in (.) DO (
   PUSHD %%a
   set currentfolder=%%~na
   magick "*.{png,jpg,tif}" "%currentfolder%.pdf"
   POPD
)
I get a "-0" file, with no extention.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining JPGS in folders and subfolders into PDFs

Post by snibgo »

You need delayed variable expansion. Add a line "setlocal enabledelayedexpansion" at the top, and use !currentfolder! instead of %currentfolder%.
snibgo's IM pages: im.snibgo.com
keljnr
Posts: 11
Joined: 2018-10-16T03:29:47-07:00
Authentication code: 1152

Re: Combining JPGS in folders and subfolders into PDFs

Post by keljnr »

Absolute Legend! Thanks for all you're help Snibgo. Thats perfect!
Post Reply