conditional "IF" statement based on filename case insensitivity

Started by jillc, January 21, 2019, 10:50:49 PM

Previous topic - Next topic

jillc

Thanks for such a great tool!  Exiftool is powerful. 

We're working on a script that reads and rewrites metadata for existing files.  Our goal is to remedy an issue with historic files that originated on an Apple OS that used resource forks by default.  We need to get rid of the references to an existing thumbnail which were stored in a resource fork.  There is a bug in modern versions of OS X that causes the Finder to keep the file locked when it is trying to read and display these broken thumbnails via SMB.

By removing the old references in the metadata, Mac OS notices the lack of a thumbnail and creates a new thumbnail and stores it safely in the modern method.  We are also fixing a few issues like missing file extensions.  We need to preserve modification dates so we are using -P.  We are also using -overwrite_original in order not to trigger the bug in OS X that causes the file to be locked, that I mentioned earlier.

We've run into a problem when files have a upper case letter in the name or extension, the script is failing with an error like this:
Error: './Forms/Photos/Img_031518/88.152_C.jpg' already exists - ./Forms/Photos/Img_031518/88.152_C.JPG

Is there anyway of making exiftool read and re-write the metadata when a file name includes a file extension without changing the existing filename's case but read and re-write the metadata and add a file extension when an extension is missing, in the same command?

Our current command is this:  exiftool -all= -tagsfromfile @ -all:all -P -r -unsafe -icc_profile '-filename<%f.$fileTypeExtension' -overwrite_original -error -ext '*' . > "/Users/admin/output-$(date +%Y-%m-%d_%H:%M).txt" 2>&1

Thank you.

Phil Harvey

This is a bit tricky, but you could do this:

exiftool -all= -tagsfromfile @ -all:all -P -r -unsafe -icc_profile '-filename<%f.${filetypeextension;$_=undef if $self->GetValue("FileName")=~m(\.$_$)i}' -overwrite_original -error -ext '*' . > "/Users/admin/output-$(date +%Y-%m-%d_%H:%M).txt" 2>&1

This will avoid renaming the file if the extension is unchanged (leaving the case of the original extension).

- Phil

Edit: Thinking about this, I just came up with a way that I could rename files like this on a case-insensitive filesystem.  I'll look into adding this ability in the next release.
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

jillc

Hi Phil,

When I run the command, every file that it encounters generates a warning:
Warning: [minor] Advanced formatting expression returned undef for 'FileTypeExtension' - ./48.E001.2_/48.E001.2-6_14.tif

Even though it sends this warning, the command does appear to solve the original problem I  posted.  It is in fact correcting the metadata for all the files, regardless of upper case or lower case extensions.  It is also properly adding an extension when a file doesn't have an extension and appending the proper extension when a file has the wrong extension.  So in every way it appears to be OK.  Were you expecting this error message or does it mean that something else might be wrong?  I tried moving the $ inside the curly brackets (like '-filename<%f.{$FileTypeExtension;) but then I just got a different error:

Warning: [minor] Tag '_' not defined - ./48.E001.2_/48.E001.2-6_14.tif

At the risk of being pedantic, I am attempting to control any errors of sampling we have seen and not.

Thank you again for this tool, and your support of the tool.

jillc

Additional information, when the testing script.  Here raises a new issue.

When a file is named Foo.jpg, the file is processed correctly and the proper extension is appended to the name of the file.  So if Foo.jpg is actually a tiff, it gets renamed to Foo.jpg.tif.

However, if the file is named "Foo.001.AA" and another file is called "Foo.002.AA", exiftool is stripping everything after the first period and then failing to write the second file in this series since its name will be "Foo.tif" just like the first file it processed.

I am not sure what is causing exiftool to strip these characters when it doesn't recognize the extension and not strip them when it does recognize them.  Can you add any modification that will remedy the file set presented?

Thank you Phil!


StarGeek

Quote from: jillc on February 06, 2019, 09:09:53 PM
When I run the command, every file that it encounters generates a warning:
Warning: [minor] Advanced formatting expression returned undef for 'FileTypeExtension' - ./48.E001.2_/48.E001.2-6_14.tif

You can suppress this by adding the -m (ignoreMinorErrors) option.  This should only happen when the extension is correct in the first place.  It is to be expected.

As for your second problem, I can't replicate it here on Windows. Can you copy/paste the exact command and output here, as well as what version of exiftool?

While I didn't include the metadata rewriting, it seems to work correctly for me.
C:\>exiftool -g1 -a -s "-testname<%f.${filetypeextension;$_=undef if $self->GetValue('FileName')=~m(\.$_$)i}" "Y:\!temp\!abr\Foo*"
'Y:/!temp/!abr/Foo.001.AA' --> 'Y:/!temp/!abr/Foo.001.jpg'
'Y:/!temp/!abr/Foo.002.AA' --> 'Y:/!temp/!abr/Foo.002.jpg'
    0 image files updated
    2 image files unchanged

C:\>exiftool -g1 -a -s "-filename<%f.${filetypeextension;$_=undef if $self->GetValue('FileName')=~m(\.$_$)i}" "Y:\!temp\!abr\Foo*"
    2 image files updated

C:\>dir "Y:\!temp\!abr\Foo*"
Volume in drive Y is DrivePool
Volume Serial Number is 3881-4F27

Directory of Y:\!temp\!abr

2018-03-03  02:08 PM            14,684 Foo.002.jpg
2018-03-03  02:08 PM            14,684 Foo.001.jpg
               2 File(s)         29,368 bytes
               0 Dir(s)  972,732,760,064 bytes free

This may need to wait until Phil comes back from vacation.  You can click the "Notify" button just above your first post to receive an email when there is a response to this thread.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).