Hi all,
Apologies in advance for my basic query (I have searched for an answer on these forums but I could not spot it!)
- On a Mac terminal I need to rename an entire directory of files from MA_dddddddd_some_file_name to just the (d) digits. e.g. MA_012345678_My_Image to just 012345678
- Also I want to place the _some_file_name part into the Headline but minus the first underscore and using spaces instead of underscores between the words? e.g. MA_012345678_My_Image to add a Headline of My Image
I know this uses wildcards and text formatting and I am not great at these - any resources on these would be amazing. Thank you for any help you can offer.
This involves some fairly advanced operations. Try this on a test directory to see if it does what you want:
exiftool "-originalfilename<filename" "-filename<${filename;/(\d+)/ ? $1 : undef}.%e" "-headline<${filename;s/.*?\d+_//;s/(.*)\..*/$1/;tr/_/ /}" DIR
Note that I am also writing OriginalFileName to preserve the original name in case you need it later.
To explain some things:
${filename;/(\d+)/ ? $1 : undef}.%e
- takes only the first string of digits from the file name, then adds back the extension (.%e).
${filename;s/.*?\d+_//;s/(.*)\..*/$1/;tr/_/ /}
- removes everything up to and including the first set of digits plus an underscore, then removes the file extension and translates underlines to spaces.
Read about Perl regular expressions (https://www.regular-expressions.info) if you want to learn more about how this was done.
- Phil
Hi Phil,
This is amazing thanks!
The headline part works great but the filename is duplicating and adding a second extension e.g. .jpg.jpg
I will certainly check the article. Thanks for your help, very much appreciated.
Ah, right. Sorry. Try this:
exiftool "-originalfilename<filename" "-filename<${filename;$_ = /(\d+)/ ? $1 : undef}.%e" "-headline<${filename;s/.*?\d+_//;s/(.*)\..*/$1/;tr/_/ /}" DIR
I forgot to set $_ to the result of the condition. In ExifTool advanced formatting expressions, you modify the default variable ($_) to change the value.
- Phil
Hi Phil,
Getting a bad substitution warning now.
Honestly Phil if it is advanced/tricky I don't want to take up any more of your time. I'll find a workaround that doesn't require that hard a line of code (for me!)
Adding "$_ =" shouldn't have caused a "bad subsitution" warning. Something else must have changed. Can you paste in the command line you used, showing the warning?
Don't worry about wasting my time. We should be really close to a solution.
I doubt that you'll find any other solution that will do exactly what you want this easily.
- Phil
Hi Phil,
Again thanks.
I get two errors - first filename not found. This goes away when I remove the original filename protection and then bad sub
You need a space before /Users/font/Desktop/Test
Also, use plain ASCII single quotes ('), not the funny quotes you are using.
- Phil
Thanks! That worked.
No idea why the odd quotes appeared (standard Mac single quote on extended keyboard). I copied your quotes and it worked.