Renaming files by removing specific text

Started by Alyssa, October 23, 2022, 12:16:38 PM

Previous topic - Next topic

Alyssa

Hello / I'm trying to remove some specific text from files coming from a certain site,I've tried something like this:

exiftool -P -overwrite_original "-filename<${filename;s/\whatever.com\././}" -k .

However, I must have messed up something because it doesn't work... any help would be appreciated^^

StarGeek

The part of the command you're using to remove text is a Regular Expression (RegEx), which can be complicated.  One big problem with that command is that you're putting a \w at the beginning and prefixing a character with a backslash can give the next character a special meaning.  In this case, \w means any "Word" character, which are A-Z, a-z, 0-9, and underscore.

The characters you have to backslash because they have special meanings are
. ^ $ * + - ? ( ) [ ] { } \ | — /
Otherwise, you don't want to include a backslash when you want an exact character match.

Additionally, you're replacing all that with a dot.

The format for a substitution is
s/SEARCH/REPLACE/
Where SEARCH is what you're searching for and REPLACE is what you're replacing it with.  If your search term needs to be case insensitive, then you would add a trailing i
s/SEARCH/REPLACE/i

The basic command you would use to remove text from a filename would be this, using TestName first to see what the results would be before actually renaming the files.  The dot in this needs the backslash because it is a special character, as mentioned above.
exiftool "-TestName<${filename;s/whatever\.com//i}" -k .

If you are using Mac/Linux/Windows PowerShell, you would want to change the double quotes " into single quotes '.

You don't need the -overwrite_original option for this command, as it doesn't actually edit the file, it is only changing file system data.  The same with the -P (-preserve) option.

If the results from using TestName look correct, you would then replace that with FileName to do the actual renaming.  Though there's no harm in including them, they just won't do anything.

If you want to learn more about RegEx, then Regular-Expressions.info is a good tutorial site.
* 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).

Alyssa