Substitute / Remove *separate* characters from filename

Started by Alyssa, November 06, 2022, 09:09:01 AM

Previous topic - Next topic

Alyssa

Hello

I'm trying to remove certain characters from a filename, but I haven't figured out how to specify separate elements (for example the letter "g" and "t").

I known the base formula is

exiftool "-filename<${filename;s/g//i}" .
(This one removes the letter "g" from a filename)

However if I try to a different letter

exiftool "-filename<${filename;s/g//i}" "-filename<${filename;s/t//i}" .

It only changes the last one ("t" in this case) - how I can specify separate letters / words in the same string?

Thanks for the help

StarGeek

You're running into Note #1 under the -TAG[+-^]=[VALUE] option (emphasis mine).

    Many tag values may be assigned in a single command. If two assignments affect the same tag, the latter takes precedence

What you need to do is combine both substitutions.  This can get into the complexities of Regular Expressions (RegEx) so I'll list both ways you can do it.

The simple way is to put both substitutions in the same tag operations separated by semi-colons
exiftool "-filename<${filename;s/g//i;s/t//i}" .

This basically says first, remove the g, then remove the t.

For the more complex regex, you can use the pipe character | as a This or That option
exiftool "-filename<${filename;s/g|t//i}" .

Two additional points.  The trailing i in this regex indicates that the matching will be case insensitive. So it will match either G or g and T or t.  Also, the listed RegEx will match only the first match.  So a filename of "Talk to the hand.jpg" would end up as "alk to the hand.jpg".  If you wanted to replace all of the ts, then you would add a g (for global) at the end like the i.  so "-filename<${filename;s/t//ig}" would end up as "alk o he hand.jpg".  You also have to be careful because if you used g with the global setting, it would also affect the g in jpg.
"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

Alyssa

Thank you very much :D that worked  ;D

(Edit: Is there a way to use the "g" argument without affecting the file extension?)

StarGeek

Quote from: Alyssa on November 06, 2022, 03:40:46 PM(Edit: Is there a way to use the "g" argument without affecting the file extension?)

Make sure your exiftool is up to date and you can use Basename and %e to re-add the extension.
exiftool "-filename<${Basename;s/g//ig;s/t//ig}.%e" .
"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

Alyssa

Quote from: StarGeek on November 06, 2022, 09:01:48 PM
Quote from: Alyssa on November 06, 2022, 03:40:46 PM(Edit: Is there a way to use the "g" argument without affecting the file extension?)

Make sure your exiftool is up to date and you can use Basename and %e to re-add the extension.
exiftool "-filename<${Basename;s/g//ig;s/t//ig}.%e" .

Using the latest 12.49 exiftool version I tried with a test file named "tt zz gtg.jpg" and after the script was now named "zz .e" it removed the "j" and "p" from the extension even tho only the "g" and the "t" were supposed to be removed  :?:

Phil Harvey

I get this:

> exiftool '-testname<${Basename;s/g//ig;s/t//ig}.%e' tmp/tt\ zz\ gtg.jpg
'tmp/tt zz gtg.jpg' --> 'tmp/ zz .jpg'

(note that I use single quotes because I'm on Mac.)

- Phil
...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 ($).

Alyssa



I get the same, but the filename remains unchanged

StarGeek



You're using fancy quotes.  You need to use regular quotes "  Also, you forgot the percent % sign in front of the e.  Or it's a BAT file and you're running into FAQ #27

One thing I have noticed is that sometimes when you quote a previous post here, there will be fancy quotes shown in the text box, but these end up as regular quotes when actually posted. Drove me mad for a day as I kept having to do a find/replace until I realized the board changed them upon posting.
"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

Alyssa

Oh yeah I was using a BAT file, the issue was with the % symbol (those weren't fancy quotes it's my windows terminal uses a weird font) - thanks again for the help, it works now  ;D