How do I merge files with partial filename matches

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
IRGT
Posts: 6
Joined: 2017-04-21T07:35:27-07:00
Authentication code: 1151

How do I merge files with partial filename matches

Post by IRGT »

This is probably easy but I'm having trouble finding the proper commands and scripting isn't my expertise. I've gone through the examples and searched the documentation multiple times over the past couple days. Any assistance is greatly appreciated.

I'm trying to convert and merge image files based on a partial match of the file name. I've been able to script entire directories and target specific filetypes for conversion/mogrify and merging, but now I'm looking to target those based on matching characters of a filename. My example below might better explain it.

Source file list - format is "type_name_pagenumber.filetype":
fruit_orange_1.tif (single page)
fruit_orange_2.tif (single page)
fruit_orange_3.tif (single page)
fruit_banana_1.tif (single page)
fruit_banana_2.tif (single page)
fruit_apples_1.pdf
fruit_tomato_1.pdf

End result I'm aiming for:
fruit_orange.tif (combined multipage - 3 pages)
fruit_banana.tif (combined multipage - 2 pages)
fruit_apples_1.pdf (untouched)
fruit_tomato_1.pdf (untouched)

In the above example, the TIF files have a type prefix (fruit) that will remain the same forever but the following name (orange) and page number (#) will change daily. I'd like to merge all files that have a matching "type_name_" in the filename of the .TIF filetype. The filenames are a fixed length, down to the type, name, page number and filetype.

After the files that belong together are together, I'd convert them to PDF, which I'm already familiar with.

Is the above possible?
IRGT
Posts: 6
Joined: 2017-04-21T07:35:27-07:00
Authentication code: 1151

Re: How do I merge files with partial filename matches

Post by IRGT »

Forgot to mention, we are running IM-7.0.4-Q16.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I merge files with partial filename matches

Post by snibgo »

I suspect this is most easily done with your script language. Which you haven't mentioned.
snibgo's IM pages: im.snibgo.com
IRGT
Posts: 6
Joined: 2017-04-21T07:35:27-07:00
Authentication code: 1151

Re: How do I merge files with partial filename matches

Post by IRGT »

snibgo wrote: 2017-04-21T09:19:09-07:00 I suspect this is most easily done with your script language. Which you haven't mentioned.
I'm trying to leverage powershell/batch/commandline, as this is a portion of some larger automation I have planned.
IRGT
Posts: 6
Joined: 2017-04-21T07:35:27-07:00
Authentication code: 1151

Re: How do I merge files with partial filename matches

Post by IRGT »

Let me ask the following, as it might help me down the right path to getting this scripted with powershell.

If I want to put multiple single page tif files and put them into a multipage tif, what is the best IM command and syntax for targeting multiple files by name?

If I want to put multiple single page tif files and put them into a multipage pdf, what is the best IM command and syntax for targeting multiple files by name?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How do I merge files with partial filename matches

Post by Bonzo »

I have never tried it but you may be looking for: https://www.imagemagick.org/script/comm ... php#adjoin

So I guess the code would look something like:

Code: Select all

convert input1.tiff input2.tiff input3.tiff -adjoin output.tiff
You might be able to use a text file for the input or create a command using a concat type function and a loop over the files in your code.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I merge files with partial filename matches

Post by snibgo »

Or you could do it with wildcards.

For IM v6:

Code: Select all

convert fruit_orange_*.tif  fruit_orange.tif
For IM v7:

Code: Select all

magick fruit_orange_*.tif  fruit_orange.tif
If each input set has a _1.tif, that may be a convenient way of finding what sets exist.
snibgo's IM pages: im.snibgo.com
IRGT
Posts: 6
Joined: 2017-04-21T07:35:27-07:00
Authentication code: 1151

Re: How do I merge files with partial filename matches

Post by IRGT »

Thanks for the suggestion on wildcards, but both the "orange" and number parts of the file name will always be changing and dynamic in nature. It is a fixed length but I will never know what those file names will be.

And while my example uses it, the number portion is actually a six character hex value, that is also dynamic but can't be used for sorting out the files, like the first 13 characters can. I just put it as a single digit to simplify the example.
IRGT
Posts: 6
Joined: 2017-04-21T07:35:27-07:00
Authentication code: 1151

Re: How do I merge files with partial filename matches

Post by IRGT »

Bonzo wrote: 2017-04-25T12:12:32-07:00 I have never tried it but you may be looking for: https://www.imagemagick.org/script/comm ... php#adjoin

So I guess the code would look something like:

Code: Select all

convert input1.tiff input2.tiff input3.tiff -adjoin output.tiff
You might be able to use a text file for the input or create a command using a concat type function and a loop over the files in your code.
Thanks! That is the format I'm using when manually running the command against test files, so that helps confirm I'm doing something right.

Now I just need to figure out the powershell portion of sorting out files and piping them into the convert command with the right format.
Post Reply