Page 1 of 1

Combining JPGS in folders and subfolders into PDFs

Posted: 2019-06-28T07:24:19-07:00
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.

Re: Combining JPGS in folders and subfolders into PDFs

Posted: 2019-06-28T08:05:05-07:00
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.

Re: Combining JPGS in folders and subfolders into PDFs

Posted: 2019-07-01T02:05:05-07:00
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.

Re: Combining JPGS in folders and subfolders into PDFs

Posted: 2019-07-01T04:25:54-07:00
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.

Re: Combining JPGS in folders and subfolders into PDFs

Posted: 2019-07-01T05:28:56-07:00
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.

Re: Combining JPGS in folders and subfolders into PDFs

Posted: 2019-07-01T06:09:49-07:00
by snibgo
You need delayed variable expansion. Add a line "setlocal enabledelayedexpansion" at the top, and use !currentfolder! instead of %currentfolder%.

Re: Combining JPGS in folders and subfolders into PDFs

Posted: 2019-07-01T06:20:35-07:00
by keljnr
Absolute Legend! Thanks for all you're help Snibgo. Thats perfect!