ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: Stephen Marsh on August 02, 2018, 11:55:51 PM

Title: Robust Invalid Character Cleaner
Post by: Stephen Marsh on August 02, 2018, 11:55:51 PM
I am trying to build a robust or "bullet proof" cross platform tag cleaner, for use in situations as as:

exiftool '-filename<${pseudotag1}_TextString_${pseudotag2}.%e' -r 'path to top level folder or file'


I am aware of ${pseudotag1;} however that does not go far enough. I really only require alphanumeric hyphens underscores to be retained, all else can go.

The source tag may contain:

!@#$%^&*(){}[]'":;<>?~`|\/=+-,.

I have tried a number of regular expressions, however I have had problems with some (a new directory is created under the user account and the files are moved there).

The best that I have come up with is:

exiftool '-filename<${pseudotag1;s/[^A-z\s\d][\\\^]?//g}_TextString_${pseudotag2;s/[^A-z\s\d][\\\^]?//g}.%e' -r 'path to top level folder or file'


Which results in a filename of:

PseudoTag1[]`_TextString_PseudoTag2[]`.tiff

I can't figure out how to remove the unwanted []` in a single regex command.
Title: Re: Robust Invalid Character Cleaner
Post by: Stephen Marsh on August 03, 2018, 01:08:57 AM
OK, some search and experimentation bought me this:

${pseudotag;s/\W+//g}

I am happy to have underscores, and alpha numeric.
Title: Re: Robust Invalid Character Cleaner
Post by: Phil Harvey on August 07, 2018, 07:35:00 AM
Hi Stephen,

I would have suggested this:

tr/-_0-9a-zA-Z//dc

What you have done will have similar results except it isn't as efficient and hyphens will also be removed.

- Phil
Title: Re: Robust Invalid Character Cleaner
Post by: Stephen Marsh on August 07, 2018, 10:44:30 PM
Thank you Phil, of course your suggestion is much better!

I had a few false starts, however I finally got the syntax right:

{pseudotag;tr/-_0-9a-zA-Z//dc}
Title: Re: Robust Invalid Character Cleaner
Post by: Stephen Marsh on February 08, 2019, 08:16:22 AM
For future reference, a similar and related topic thread that has very useful information relating to this topic can be found here:

https://exiftool.org/forum/index.php/topic,9857.0.html