ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: empedia on February 15, 2013, 12:44:39 PM

Title: Composite tag to convert whitespace to _ except trailing or leading whitespace
Post by: empedia on February 15, 2013, 12:44:39 PM
Hi,

I am trying to use the rename functionality to rename some files based on the value of the tag but the value of the tag sometimes has one or more trailing spaces or leading spaces in it.

I need to convert any spaces in the body of the tag value to underscore but I also want to get rid of any trailing  or leading whitespace completely.

I understand I need to create a user-defined (composite) tag to do this but I don't have the knowledge of Perl substitution to know how to specify the expression I need to achieve this.

Can anyone help?

Many thanks,

Matthew.
Title: Re: Composite tag to convert whitespace to _ except trailing or leading whitespace
Post by: Phil Harvey on February 15, 2013, 12:58:44 PM
Hi Matthew,

As of ExifTool 9.15 you can now do this with an advanced formatting expression.  Specifically, this command should do what you want:

exiftool "-filename<${TAG;s/(^\s+|\s+$)//g;tr/ /_/}.%e" FILE

where TAG is your tag name, and FILE is one or more directory or file names.  The above quoting is for Windows.  Use single quotes instead if you are on Mac or Linux.

- Phil

Edit: Fixed a couple of slashes that should have been backslashes
Title: Re: Composite tag to convert whitespace to _ except trailing or leading whitespace
Post by: empedia on February 18, 2013, 07:25:56 AM
That works great thanks!