Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Gigolo Joe

macrumors newbie
Original poster
Jan 26, 2007
11
5
London
I have numerous differently named files and want to batch remove all characters after months or years included in the filenames.

I can't find any discussions about this scenario, let alone any renaming tools that do much more than remove a set number of characters from the end.

To be clear, every filename is completely different so I can't easily search and remove. Relevant info is more or less there up to the dates, so everything after is redundant.

Is there a tool that can do this, or even a terminal formula? I may be missing something really obvious, so feel free to point and laugh.
 

Lifeisabeach

macrumors 6502
Dec 4, 2022
352
371
You could try a renaming tool that uses regular expressions. For example, I use Name Mangler. I just took a couple wallpaper photos I have and inserted March into the middle of the file name, then changed up the number and types of characters afterwards. Next, using Find and Replace with "Query is a regular expression" checked, I used this exact string:

Code:
March.......

Take note of the period character. That's basically a wildcard. Now, in this case, it will only rename if the file has that exact number of characters after "March", but you could always keep running this by removing one period at a time until you get them all. And there are probably better ways to use regular expressions for your need... I'm not particular adept at them. Here's a Wiki page on how to use them:
Code:
 

Gigolo Joe

macrumors newbie
Original poster
Jan 26, 2007
11
5
London
Yes. Post a bunch of examples of file names and I can perhaps post some command line magic you can work with.

Thanks for the quick reply.. How about these 3 typical examples:

Henley 06/12 jwt review 19.jpg

santa_cruz_2011_empty_filename.ext.png

FLAT PICS 7 Aug 2009 IMG_8048_h1rz.JPG

Each has a date/month/year after which there's loads of junk.
 

Gigolo Joe

macrumors newbie
Original poster
Jan 26, 2007
11
5
London
You could try a renaming tool that uses regular expressions. For example, I use Name Mangler. I just took a couple wallpaper photos I have and inserted March into the middle of the file name, then changed up the number and types of characters afterwards. Next, using Find and Replace with "Query is a regular expression" checked, I used this exact string:

Code:
March.......

Take note of the period character. That's basically a wildcard. Now, in this case, it will only rename if the file has that exact number of characters after "March", but you could always keep running this by removing one period at a time until you get them all. And there are probably better ways to use regular expressions for your need... I'm not particular adept at them. Here's a Wiki page on how to use them:
Code:

Again, thanks for this quick reply. I tried, and it does work, however it takes almost as long to keep adding or deleting dots - plus renaming etc.. than it does to simply manually remove end characters in Finder. Although maybe there's another step I'm missing, so perhaps I'll experiment.
 

Lifeisabeach

macrumors 6502
Dec 4, 2022
352
371
Again, thanks for this quick reply. I tried, and it does work, however it takes almost as long to keep adding or deleting dots - plus renaming etc.. than it does to simply manually remove end characters in Finder. Although maybe there's another step I'm missing, so perhaps I'll experiment.

Try using just one dot, maybe 2. And just run it repeatedly until it stops hitting. That will go a lot faster.
 

Bigwaff

Contributor
Sep 20, 2013
2,022
1,318
Henley 06/12 jwt review 19.jpg

santa_cruz_2011_empty_filename.ext.png

FLAT PICS 7 Aug 2009 IMG_8048_h1rz.JPG
And so you want the file names to be …
Henley 06/12.jpg
santa_cruz_2011.png
FLAT PICS 7 Aug 2009.JPG

Yes? May I ask why? Even after removing the “redundant” text at the end, the file names are still formatted randomly and thus hard to order/search/find/etc.
 

Gigolo Joe

macrumors newbie
Original poster
Jan 26, 2007
11
5
London
And so you want the file names to be …
Henley 06/12.jpg
santa_cruz_2011.png
FLAT PICS 7 Aug 2009.JPG

Yes? May I ask why? Even after removing the “redundant” text at the end, the file names are still formatted randomly and thus hard to order/search/find/etc.
Of course you can. These are 3 different examples of typical filenames with irrelevant info or improper formatting. I have many hundreds of each type - ie - relevant info + date, or relevant info + month or year.

Most of these have had naming conventions added to the beginning. I need to clean them up to more easily search, sort, or list quickly in finder and remove legacy naming conventions or random junk. Especially words that show up in searches incorrectly.

With each filename type, I'd like to key off either the date "06/12", month "Aug", or year "2011", removing everything that comes after.

There will be many with a regular expression - same month, same year, same date, so I imagine that's straightforward. But it would be even better if there's a way to recognise that there is a date in the fiename, and key off that.
 

Fishrrman

macrumors Penryn
Feb 20, 2009
28,525
12,651
I'm thinking...
... there are some jobs that you just have to do "by hand"...
 

Lifeisabeach

macrumors 6502
Dec 4, 2022
352
371
There will be many with a regular expression - same month, same year, same date, so I imagine that's straightforward. But it would be even better if there's a way to recognise that there is a date in the fiename, and key off that.

That would require some sort of tuned AI or fuzzy logic, none of which may exist. You could probably cobble together some kind of complex script to do it, but quite frankly you'd be done by now using basic regular expression schemes.

Using Name Mangler or whatever your choice is... Find and Replace using "March." (without the quotes) and replace with "March". Click on rename repeatedly until everything is done. It would take just a few seconds. Speed it up using 2 periods with a final single period for stragglers.

I would use Finder's search function to find just one month like that at a time. When you highlight it all, run Name Mangler. NM can automatically load up everything in that Finder window (just set the startup preference to populate the Finder selections). Super easy. This way you can eyeball the results, and revert if you realize you messed something up.
 

Gigolo Joe

macrumors newbie
Original poster
Jan 26, 2007
11
5
London
After many many forum questions, and a lengthy exchange with ChatGPT, I finally found a solution to this that seems to work for any specified characters, including 'special' characters. For example, here is terminal command for deleting everything following "1984" in multiple filenames, while retaining the file ext:

find . -name '*1984*' -type f -exec sh -c 'mv "$1" "$(echo $1 | sed -E "s/1984(.*)(\.[^\.]+)$/1984\2/")"' _ {} \;

Simply change each occurrance of this year to any final character(s) you want to keep. Special characters will most likely need a "\" placed before it in each of the 3 places.

This command removes the last occurance of the specified character(s), and everything else up to the .ext, then adds the specified character(s) back in.

I haven't found a command that automatically keys off any year or month or date detected, so I'd be grateful if anyone has suggestions.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.