Tricky File Rename - Removing Part of Name

Hi All!
First-time poster but long-time listener/follower. I have a tricky piece of my work that I’m trying to automate and am looking for suggestions. I feel like the answer is probably a Hazel action, but I can’t figure it out. I have Word docs that need to be renamed on a regular basis. The current filename structure is Transplant - First Last - Last2, First2.doc and I’d like them to read Transplant - Last2, First2.doc.

So, to be clear, my files look like:
Transplant - John Smith - Brown, Susan.doc
Transplant - Kate Johnson - Richardson, Bob.doc

and I’d like them to read:
Transplant - Brown, Susan.doc
Transplant - Richardson, Bob.doc

Any ideas on how I could accomplish this with Hazel? Or anything else?
Thanks for any advice!

Hi @Annie_Kuhl — Welcome to the forum!

Your file rename can be done with Hazel and a Shortcut. One of Hazel’s actions is to be able to run a Shortcut.

I’ll have a proper look at it tomorrow when I can get to my Mac, but in the mean time here is a Shortcut that does the rename using a regex pattern in a replace action. For the sample, I have just provided the Shortcut with one of your file names as a text string at the start of the Shortcut — obviously this will be replaced by the filename that Hazel is working on.

Sample RegEx filename replace

I hope this helps and I’ll have a better look at Hazel tomorrow.

Wow! Thank you for the quick reply- I really appreciate your help! I think I’m still on the struggle bus on how to use this correctly since the names are changing in each file …so much for me to learn yet :blush:!

Keyboard Maestro can support this or you might try a dedicated (free) app like Transnomino

While @Tony’s shortcut will probably work fine, I can think of a couple of situations in which it won’t: names with accented characters and hyphenated names. If you might need to handle those situations, it would be better to use a shortcut that splits the filename on space-hyphen-space and then builds a new filename from the first and last parts. Like this:

Rename With Split

1 Like

Here is a solution that should work. I have swapped out the regex pattern matching for a Split Text (as suggested by @drdrang above). On saving the Shortcut you will be asked to select the folder where your files are stored.

PLEASE TEST THIS ON DUMMY FILES BEFORE ANY LIVE DATA!

Heere is a link to the Shortcut

and this is the Hazel rule that I used to run it:

(I’m not aware of a way to export a Hazel rule, hence the picture only)

If you have Keyboard Maestor, as suggested by @Artisan above, you could also perform this task in there without having to do a two app approach, but I was initially sticking to the apps that you had mentioned (assuming that you are on macOS Monterey…).

I hope this helps.

Edit - Just in csse you haven’t used Shortcuts with Hazel before, my M1 MacBook Air beach balls for a good few minutes when you select the Run Shortcut action in Hazel as it reads in the Shortcuts before allowing you to select the required one. It also does this any time that you edit any part of the rule.

The Hazel rule will not match if you have used Hazel on the doc file before (for example to move the file to the current folder) as I used the previously matched date to stop the rule matching the same file again and again.

Edit 2:
I made a slight error in the original Shortcut above that ommitted the Transmit - from the start of the new filename; this versioin corrects that. Sorry about that… in my defence, instead of waiting overnight and doing the Shortcut today as I originally said, i got intruiged and wrote it at 4am.

Python3 solution, assuming you’re putting this as a python file in the same folder as the Word docs. Disclaimer: If you don’t know what this does please don’t use it, copying random code from the Internet is dangerous. May be useful to Future People™ in either case.

from pathlib import Path

files = Path.cwd().iterdir()

word_docs = []
for item in files:
    if item.is_file() and item.suffix in ['.doc', '.docx']:
        word_docs.append(item)

for doc in word_docs:
    name_parts = doc.parts[-1].split(' - ')
    new_name = f'{name_parts[0]} - {name_parts[2]}'
    doc.rename(new_name)
    print(f'Renamed {doc.parts[-1]} to {new_name}')

I prefer A Better Finder Rename for complex renaming tasks like this. You don’t have to write software to make it work, and you can always preview the results before committing.

4 Likes

This is MAGICAL! Thank you so much for your help with this- it has literally saved me so much time this morning in getting letters out to transplant recipients :smile:!

And now I need to do a deep dive into Shortcuts and Hazel to understand all of this more so I can come up with clever solutions on my own. Thank you all again - what a wonderful and helpful community of people!

2 Likes

Glad it helped; it was a fun one to come up with.

This can be done in any tool that supports Rename features like Path Finder, A Better Finder Rename. Renamer etc. Any software that can support Regular Expressions or Renaming capabilities.

The steps should be similar, but I would warn you to test it out on some copies of files in a separate directory/folder till you get comfortable.