Page 1 of 1

How do I merge files with partial filename matches

Posted: 2017-04-21T08:03:18-07:00
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?

Re: How do I merge files with partial filename matches

Posted: 2017-04-21T08:16:35-07:00
by IRGT
Forgot to mention, we are running IM-7.0.4-Q16.

Re: How do I merge files with partial filename matches

Posted: 2017-04-21T09:19:09-07:00
by snibgo
I suspect this is most easily done with your script language. Which you haven't mentioned.

Re: How do I merge files with partial filename matches

Posted: 2017-04-21T10:10:00-07:00
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.

Re: How do I merge files with partial filename matches

Posted: 2017-04-25T12:03:07-07:00
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?

Re: How do I merge files with partial filename matches

Posted: 2017-04-25T12:12:32-07:00
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.

Re: How do I merge files with partial filename matches

Posted: 2017-04-25T12:18:15-07:00
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.

Re: How do I merge files with partial filename matches

Posted: 2017-04-25T12:24:25-07:00
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.

Re: How do I merge files with partial filename matches

Posted: 2017-04-25T12:25:57-07:00
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.