extracting part of the filename and repositioning it

Started by ibex, April 23, 2024, 01:20:11 AM

Previous topic - Next topic

ibex

I have a series of files that are named in the format of a short alpha string of variable length followed by an optional special character and then a numeric of some sort e.g. and then IMG_..., IMG..., DSC...,  PXL_..., MVIMG..., PANO... etc

I want to extract the alpha string and as part of renaming the file into a YmdHMS format I want to add that alpha string to the end of the filename along with the -%c in case of multiple hits on the same name.

I've got the command worked out to rename the file and build the date and location driven directory tree but string manipulation has got me beat.

I have found many examples, such as the randomly selected one below, where I think that string manipulation and substitution is happening but I don't understand them.

I'd really appreciate it if someone could help me with it.


Another alternative is to use the new advanced formatting option that was introduced in ExifTool 9.15, and a command like this:

exiftool "-title<${filename;s/\.[^.]*$//}" -ext jpg



Phil Harvey

Something like this?

exiftool "-testname<datetimeoriginal" -d "%Y%m%d%H%M%S_%%3f%%-c.%%e" DIR

If this gives you the names you want, then replace "testname" with "filename" to actually do the renaming.

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

StarGeek

Quote from: Phil Harvey on April 23, 2024, 07:44:04 AMSomething like this?

I think they are looking to understand the RegEx involved in the Advanced Formatting Feature rather than the simplier % because their examples include text that can be 3 or 4 characters long.

@ibex, the Advanced Formatting in your example uses Perl's Regular Expressions (RegEx) to manipulate the filename. It is a complex subject that really can't be taught here.  But there are a lot of sites that do teach about it, even whole books on the subject.

At its very simplest, the structure is s/SEARCH/REPLACE/modifiers. It looks for SEARCH and replaces it with REPLACE. The power of RegEx lies within its special characters and options, which can modify SEARCH with incredible flexibility.

The way I learned was through a combination of reading Regular-Expressions.info (though there may be better sites these days), searching the various Stack Exchange sites with queries like "Perl regex <what I want to do>", and then testing the expressions on RegEx101.  The last site is particularly helpful as it will break down a RegEx step by step, though it probably doesn't work on mobile very well.

Here's a breakdown of the RegEx you listed above. You can mouse over any part of it and it will give an explanation either through a popup or in the upper right corner of the page.

To expand upon Phil's answer to give flexibility in moving the prefix text to the end, you could use something like
exiftool -d "%Y%m%d%H%M%S" "-Testname<${DateTimeOriginal}_${BaseName;s/^([A-Z])+.*/$1/i}%-c.%e" /path/to/files/

This will take the Basename (no extension), extract any leading letters A-Z, case insensitive, and remove everything else in the filename. This is appended to the DateTimeOriginal that is formatted by the -d (-dateFormat) option.

* 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).

ibex

Quote from: Phil Harvey on April 23, 2024, 07:44:04 AMSomething like this?

exiftool "-testname<datetimeoriginal" -d "%Y%m%d%H%M%S_%%3f%%-c.%%e" DIR

If this gives you the names you want, then replace "testname" with "filename" to actually do the renaming.

- Phil
Thanks Phil. 

I didn't know about the %3f option so that's really helpful for furture use, but Stargeek is right, I want to know more about the RegEx expressions and I don't have a fixed number of characters in the prefix to the filename.

ibex

Thanks @StarGeek.

You were correct, I wanted to understand how the RegEx in the Advanced Formatting worked.  I had come across RegEx years ago but didn't even recognise in those strings.

Quote from: StarGeek on April 23, 2024, 11:03:06 AMTo expand upon Phil's answer to give flexibility in moving the prefix text to the end, you could use something like
exiftool -d "%Y%m%d%H%M%S" "-Testname<${DateTimeOriginal}_${BaseName;s/^([A-Z])+.*/$1/i}%-c.%e" /path/to/files/

This almost worked - it only appended the last char of the text prefix.  But, by using the RegEx101 site you linked, I found that I needed the  RegEx to be:
  {Basename;s/^(([A-Z])+).*/$1/i}  
The extra set of brackets that incorporates the + returns the full alpha string.

Thanks to both you and @Phil for the help.  Tomorrow I'll  put up the full set of exiftool commands I'm using to build the directory tree and rename the files for the information of anyone else who might be interested.

Phil Harvey

Quote from: ibex on April 24, 2024, 05:26:16 AM {Basename;s/^(([A-Z])+).*/$1/i}  
The extra set of brackets that incorporates the + returns the full alpha string.

Just include the plus sign in the original brackets -- no need for two sets:

    {Basename;s/^([A-Z]+).*/$1/i}

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

StarGeek

Quote from: Phil Harvey on April 24, 2024, 06:55:11 AMJust include the plus sign in the original brackets -- no need for two sets:

Bah, that was a silly mistake for me to make.
* 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).

ibex

Quote from: StarGeek on April 24, 2024, 11:06:31 AM
Quote from: Phil Harvey on April 24, 2024, 06:55:11 AMJust include the plus sign in the original brackets -- no need for two sets:

Bah, that was a silly mistake for me to make.
LOL - There's whole sites dedicated to analysing the syntax and output of the command you are building so I don't think any mistake could really be considered silly.