Create a Composite Tag Conditionally

Started by dabeamer, December 26, 2018, 01:19:32 AM

Previous topic - Next topic

dabeamer

Hello there,
Long time user of exiftool, first time poster here to the forum. I have built extensive automation leveraging exiftool (and it's bailed me out numerous times), so I'm very grateful for all the hard work that has been put into this tool. With that said, I've run into a scenario (due to a new device I recently started using) that I'm not sure how to handle and would like to seek some support.

I'm attempting to create a composite tag that generates a 'file number' I can use as a part of my renaming process. Essentially, I try to take an existing "filenumber" tag first if it exists, but otherwise I resort to trying to extract it from the filename. I then use this tag in lieu of the "filenumber" tag in my renaming process. I have this working fine, but there is a corner case I'm not sure how to handle. This case occurs when neither a "filenumber" tag nor a filename number sequence is present. As-is, my .ExifTool_config returns a null string in this case, but the composite tag still exists. Ideally, the composite tag wouldn't exist at all and my file renaming process would simply fall back to the naming convention that doesn't provide a file number.

Perhaps I'm overthinking this? The question I want to ask is -- is there a way to conditionally generate a composite tag? That seems the simplest fix to my solution as-is, but I'm open to other suggestions if I'm making this too difficult.

Here is the excerpt of my .ExifTool_config:

        MyFileNumber => {
          Desire => {
            0 => 'FileNumber',
            1 => 'FileName',
          },
          ValueConv => '
            if ($val[0] ne "") {
              $val[0] = sprintf("%04d",$val[0]);
              $val[0];
            }
            elsif ($val[1] =~ /_[0-9]*/) {
              $val[1] =~ s{.*_([0-9]*).*}{$1};
              $val[1] = sprintf("%04d",$val[1]);
              $val[1];
            }
            else {
              ""
            }'


And here is my renaming setup:

picture_rename() {
  # Preserve original file date/time and overwrite the original file
  exiftool -P -overwrite_original \
  '-originalfilename<filename' \
  '-filename<${filemodifydate}%-c.%le' \
  '-filename<${filemodifydate}_${myfilenumber}.%le' \
  '-filename<${modifydate}%-c.%le' \
  '-filename<${modifydate}_${myfilenumber}.%le' \
  '-filename<${createdate}%-c.%le' \
  '-filename<${createdate}_${myfilenumber}.%le' \
  '-filename<${creationdate}%-c.%le' \
  '-filename<${creationdate}_${myfilenumber}.%le' \
  '-filename<${datetimeoriginal}%-c.%le' \
  '-filename<${datetimeoriginal}_${myfilenumber}.%le' \
  '-filename<${filemodifydate}_${mymodel}%-c.%le' \
  '-filename<${filemodifydate}_${mymodel}_${myfilenumber}.%le' \
  '-filename<${modifydate}_${mymodel}%-c.%le' \
  '-filename<${modifydate}_${mymodel}_${myfilenumber}.%le' \
  '-filename<${createdate}_${mymodel}%-c.%le' \
  '-filename<${createdate}_${mymodel}_{$myfilenumber}.%le' \
  '-filename<${creationdate}_${mymodel}%-c.%le' \
  '-filename<${creationdate}_${mymodel}_${myfilenumber}.%le' \
  '-filename<${datetimeoriginal}_${mymodel}%-c.%le' \
  '-filename<${datetimeoriginal}_${mymodel}_${myfilenumber}.%le' \
  '-filename<${filemodifydate}.${mysubsec}_${mymodel}.%le' \
  '-filename<${filemodifydate}.${mysubsec}_${mymodel}_${myfilenumber}.%le' \
  '-filename<${modifydate}.${mysubsec}_${mymodel}.%le' \
  '-filename<${modifydate}.${mysubsec}_${mymodel}_${myfilenumber}.%le' \
  '-filename<${createdate}.${mysubsec}_${mymodel}.%le' \
  '-filename<${createdate}.${mysubsec}_${mymodel}_${myfilenumber}.%le' \
  '-filename<${creationdate}.${mysubsec}_${mymodel}.%le' \
  '-filename<${creationdate}.${mysubsec}_${mymodel}_${myfilenumber}.%le' \
  '-filename<${datetimeoriginal}.${mysubsec}_${mymodel}.%le' \
  '-filename<${datetimeoriginal}.${mysubsec}_${mymodel}_${myfilenumber}.%le' \
  -d %Y-%m-%d_%H.%M.%S "$@"
}


Thanks in advance for your help!

StarGeek

It's late, so I don't have time to test your code, but instead of
else {
              ""
            }'


try using undef;.
else {
              undef;
            }'


"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

dabeamer

Thank you very much for the reply and suggestion, but unfortunately this simply results in a tag with '0000' in it. This ends up being less desirable than the empty string because '0000' is included in the filename. Thanks again, I appreciate your help.

Hayo Baan

Can you provide some examples of how the renaming should look? Include cases where your current renaming fails.

I think the easiest solution would actually be to create a composite tag that does the whole renaming for you... 
Hayo Baan – Photography
Web: www.hayobaan.nl

StarGeek

Quote from: dabeamer on December 26, 2018, 02:29:44 AM
unfortunately this simply results in a tag with '0000' in it.

Something else must be wrong, as returning undef won't end up with a result of 0, much less four 0s.

Returning undef is the correct response if you want exiftool to skip that variable.

I copied your code into my config file and I get the correct response when I swap the double quotes for undef

C:\>exiftool "-testname<${modifydate}_${myfilenumber}.%le" -d %Y-%m-%d_%H.%M.%S y:\!temp\Test4.jpg
Warning: [minor] Tag 'myfilenumber' not defined - y:/!temp/Test4.jpg
Warning: No writable tags set from y:/!temp/Test4.jpg
    0 image files updated
    1 image files unchanged
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

dabeamer

QuoteI think the easiest solution would actually be to create a composite tag that does the whole renaming for you...
I like the idea of using a consolidated composite tag, I think that's the better solution albeit with a bit more work. I've been using this setup and bolting onto it for years, so I didn't really think about it that way.

QuoteReturning undef is the correct response if you want exiftool to skip that variable.
You are correct. As you noted, it was late, and I tested on a file that gave me the bad output. I need to make my regex parsing a bit more robust, but this seems to have done exactly what I wanted originally. Now that I know this, I may check for specific sequences rather than trying to make it generic. It will be more work for me to maintain, but more repeatable and consistent for my purposes.

Thank you both for your help!