Renaming problem

Hello all - esp. the renaming wizards :slight_smile:
I'm using D9.

I have a bunch (meaning: several thousands...) of files which I would like to rename. This is the general pattern of their current name:
Firstname Lastname - Some other stuff.txt
(so Firstname space Lastname space hyphen space some other stuff dot txt)
Example: Jim Lewis - First chapter.txt

I would like to rename this into:
Lastname, Firstname - Some other stuff.txt
(so Lastname comma space Firstname space hyphen space some other stuff dot txt).
Basically: find first- and lastname, i.e. the part before the hyphen, invert them, separate them by a comma.
Example: Lewis, Jim - First chapter.txt

I'm sure some clever regex could do this. Alas, my regex knowledge is virtually non-existant... So, is there any wizard out here who could help me??

Thanks!

Old name: (\S+)\s+(\S+)(\s-\s.*)$
New name: \2, \1\3
Type: Regular Expressions


My memory of that far back is fuzzy but I think the regexps in Opus 9 wouldn't have supported \S and \s, so if the expression doesn't work in 9 then that's probably why. (Still worth a try in case my memory is wrong.)

I was concerned that might be the case, but used \s and \S anyway since space is so difficult for users to "see".

Anyway, here's the alternative for ericverboven just in case:

Old name: ([^ ]+) +([^ ]+)( - .*)$

@ericverboven - be sure to copy/paste from the opening paren to the closing $ char so that you capture the spaces correctly.

MrC and Leo, you made my day (and my night). My sincere thanks for your clever (and working!!) solution. Really, this helps me A Lot!

Just to be on the safe side for other users who might want to use the above solution: for me it works in the majority of cases, but be careful when there are more word-space-combinations involved in the first string. For instance:
John James Doe - Some stuff.txt
becomes:
Doe, James - Some stuff.txt
i.e. "John" is removed. For me, that doesn't matter, but I just wanted to point it out.

Try

Old name: ^([^ ]+.?) +([^ ]+)( - .)$

This is always a problem with using a simple word-match algorithm to detect First names vs. Surnames. There is no simple solution that works in all cases, nor often in general (depending upon name origin).

Some names are very complex: Juan Jose Montes de Oca. Which is the first name? Which is the last?

Can you answer a question for those of us who have forgotten. Did DOpus 9 support \s and \S ?

To answer MrC's request: I used this for my old names:
([^ ]+) +([^ ]+)( - .*)$
and this for the new names:
\2, \1\3

And it works just fine - with the exception of Juan Jose Montes de Oca :slight_smile: