Batch change filenames with Regex (insert a dash between second and third word)

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
louwin
Member
Member
Posts: 112
Joined: 2007-06-09, 11:22 UTC
Location: Perth, Western Australia

Batch change filenames with Regex (insert a dash between second and third word)

Post by *louwin »

I'm sorry if this is a question beyond the scope of TC support :( It is not specifically about TC but a feature of it. Just say if it is beyond scope and I'll delete the post (if possible?) :) I'm talking about the Multi Rename Tool here :)

I have a folder of files with names of undetermined words and I want to insert a dash between the second word and the third thus....

"first second third fourth fifth sixth and so on and so on" and I want to change all the file names to....

"first second - third fourth fifth sixth and so on and so on" - I wonder if the Regex experts here could detail the command to do this? Is it possible? Or should I just do it manually? I HAVE Googled it but not got a satisfactory answer :(

Thanks in anticipation :)
There are 10 types of people in the world,
Those who understand binary and those who don't.
User avatar
Stefan2
Power Member
Power Member
Posts: 4132
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: Batch change filenames with Regex

Post by *Stefan2 »

FROM:
first second third fourth fifth.txt
TO:
first second - third fourth fifth.txt

- create a backup of that files
- select all wanted files
- open MRT
- Search: ^(.+?)\s(.+?)\s
- Replace: $1 $2 -
[x] RegEx


Explanation:

^(.+?)\s(.+?)\s

^ --- start matching at begin of string
() --- store what is matched for later re-use
. --- match one of any single sign (number, char, dot , dash, space, ...)
+ --- match one-or-more of the expression on the left
? --- match non-greedy (match as few as possible only)
\s --- match one white space

(.+?)\s ---match one-or-more of any sign, but non greedy until an space, and store in capture group

$1 --- give back what is stored in first capture group, counted from the left




You can also use:
- Search: ^(.+?\s.+?)\s
- Replace: $1 -





.
User avatar
louwin
Member
Member
Posts: 112
Joined: 2007-06-09, 11:22 UTC
Location: Perth, Western Australia

Re: Batch change filenames with Regex (insert a dash between second and third word)

Post by *louwin »

Thanks Stefan2 - you really know your Regex :)

I used your first solution - worked PERFECTLY

Thanks again :)
There are 10 types of people in the world,
Those who understand binary and those who don't.
Post Reply