ExifTool Forum

ExifTool => Newbies => Topic started by: elhiero on May 14, 2024, 12:47:53 PM

Title: Characters substitution
Post by: elhiero on May 14, 2024, 12:47:53 PM
Hello,
I would like to copy some parts of filenames into Caption-Abstract.
The template for my filenames is :
20240514-12:22-My_filename_had_underscores

I would like to get in the Caption-Abstract field :
My filename had underscores

I tried this
-k -L -IPTC:Caption-Abstract<${Filename;s/(.{15})(.*)_(.*)_(.*)_(.*)/$2" "$3" "$4" "$5/}It works, but some files have 0,1,2,3, or more underscores in their names ...

I tried also -k -L -IPTC:Caption-Abstract<${Filename;s(_)(" ")}Here, only the first underscore is replaced by a space ...
Thanks
Title: Re: Characters substitution
Post by: StarGeek on May 14, 2024, 01:01:02 PM
The easiest thing to do would be to use the Perl tr operator.

exiftool -k -L "-IPTC:Caption-Abstract<${Filename;tr/_/ /}" /path/to/files/

Your second command only did one substitution because you didn't have the Global flag on it.
-IPTC:Caption-Abstract<${Filename;s(_)(" ")g}


You might want to consider using Basename instead of Filename because Basename doesn't include the extension.
Title: Re: Characters substitution
Post by: elhiero on May 14, 2024, 01:43:28 PM
Thank you very much.
I learnt today:
tr/ / /
s(_)(" ")g
basename

I can use now
-k -L -overwrite_original -IPTC:Caption-Abstract<${Basename;s/^.{15}//;tr/_/" "/}
Title: Re: Characters substitution
Post by: Phil Harvey on May 15, 2024, 03:37:38 PM
I don't think this is what you want: tr/_/" "/

The tr operator translates single characters only, so this will translate _ to ", not " ".

- Phil
Title: Re: Characters substitution
Post by: StarGeek on May 15, 2024, 04:46:11 PM
From the lack of quotes elsewhere, I'm assuming that this command is being used in Windows PowerShell.

PowerShell is so weird.  The tag copy operation doesn't need quotes around it. I've got no clue as to how it decides that this isn't a redirection operation.

But the end result is that the quotes are needed to keep the space as part of the same argument.
Title: Re: Characters substitution
Post by: Phil Harvey on May 15, 2024, 09:16:44 PM
Ah. Thanks.

- Phil