Using "Rename using RegEx" to reformat file names

I am currently facing the following issue: I have a number of files which are named like this

Firstname Lastname. Title. Year

```or

Herman Melville. Moby Dick. 1851

I would like to batch rename a number of files with this format to the following format:

Fistname Lastname. Year. Title

Herman Melville. 1851. Moby Dick

I believe this should be fairly straight forward with the RegEx rename script, if one knows what one is doing when using RegEx. Unfortunately, I have no idea what I am doing and would be greatly appreciative of any help/advice! 

Thanks!

Are there actually dots in the name, like "Herman Melville. Moby thingy. 1851” ? Hyphens or underscores in the final name would be a bteer idea too.

Yes, there are actual dots in the name! I had decided to go with dots due to legibility. Before using DevonThink, I used hyphens, given that I had to access the files via command line a lot. Is there a particular reason why you’d advise not to use dots when using DevonThink?

Yes, because of exactly what you mentioned. I would not advise changing it just for use in DEVONthink when the potential for automation exists, including calling shell scripts (which this script is doing).

Now the dots need to be escaped.
Also, there could be a lack of uniformity in use of spaces after the dot. Just one more thing to account for in the RegEx, but would not be an issue if you were simply using hyphens or underscores.

Source pattern


^(.*)\. *(.*)\. *(.*)$

Destination pattern


\1_\3_\2

This assumes you have no file extensions, per your example.

1 Like

Thanks a lot! This worked perfectly.

You’re welcome. :smiley:

I am reviving this thread because I can’t get the script to work for me. I can’t get it to do simple things. Example: let us say I want to replace the whole title for just the year.

[Centennial Book] Georgios Anagnostopoulos - Aristotle on the Goals and Exactness of Ethics (1994, University of California Press)

Source pattern (\d\d\d\d)
Replace pattern \1

Nothing happens.
Do I need a pattern that matches the whole thing and then capture what I want out of it?

  • What do you expect the source pattern to match?

  • What do you expect the output to be?

let us say I want to replace the whole title for just the year

(right now I just want to understand how it works)

Yes, I read that but it’s unclear what you meant.

Do you want to cull the 1994 from the filename and just use that instead?

Also I’m curious where you got (\d\d\d\d) as the source pattern.

There are four digits in 1994. In most regex systems I know this would match 1994, just as \d{4} would. Is this not the case? I am not used to using regex in terminal.

Yes.

RegEx is not a single standard and there are not only variants per OS but the support (or lack thereof) depends on the process at play. The script uses sed which doesn’t support \d (at least the built-in version).

[0-9]{4} would be a safer bet as that’s a more generic approach to querying numbers.

Source: .*([0-9]{4}).*
Dest: \1

1 Like

Awesome. Thanks, Jim!

I am aware. I just didn’t know what the specificities of sed were :wink:

No problem.
Yeah, and the variants can cause a bit of confusion since the implementation of RegEx between processes, e.g., sed and egrep may not be the same.

1 Like

BTW:
The next release won’t require scripts anymore, smart rules & batch processing will support this:

1 Like

That is pretty cool, Chris. Is it still a sed script or has something changed?

This won’t use sed internally.

1 Like