Renaming files according to a regex pattern

Requires an M1 Mac, though

I would like to rename files on entry to DVT by replacing - and _ with spaces. I really hate unnecessary hyphens and underscores in names.
I’d like this to happen immediately on import to the Inbox. I thought of having Hazel act on the Inbox but files may not stay there long enough.
I know nothing of RegEx.
Is this possible?

A smart rule triggered on import and executing a simple script should be able to handle this. See e.g. code of Scripts > Rename > Replace Text…

1 Like

I’m trying to rename contracts with the project name. The project name can be any combination of words, digits and characters like ’ – &. It always occurs between 'programmet ’ and (herefter kaldet Leverancen).
I built the rule on regex101 and when I run it the tag that is added is the last character of the found string, but the renaming doesn’t do anything except to add ’ - '. I’m using the placeholder %documentString%.

Can this be done?
CleanShot 2024-06-06 at 11.56.03

In case of regular expression scanning you have to use placeholders like \0, \1, \2 etc (like in your Add Tags action)

1 Like

Your regex makes no sense. (.) puts exactly one character in the first capturing group. The * must be part of the group if you want to capture more than a single character.

1 Like

Perfect, that was it! Thank you both very much :slight_smile:

1 Like

Hi. I want to rename files to remove the date at the end of the file. So I want to change:

“Example 04 September 2024” to “Example”

I’m trying to use the rename using RegEx script.

I’m using the following in the source pattern, but do not know what to put in the destination pattern:

(([1-9])|([0][1-9])|([1-2][0-9])|([3][0-1]))\ (January|February|March|April|May|June|July|August|September|October|November|December)\ \d{4}$

Your RE is far too complicated, and you didn’t describe the current naming pattern clearly. But this might fit the bill

^(.*?)\s+\d\d?\s+(January|February|…)\s+\d{4}`

Use \1 or $1 in the replacement part – that might depend on the inner working of the script.

If your filenames are conforming to this, there’s no need to do what you’re doing.

Select the file and choose Tools > Batch Process.
Use the Change Name action and use the Name without date placeholder.

image

image

1 Like

Oh dear, thanks!!

You’re welcome :slight_smile: