Rename files tool x RegEx
Moderators: Hacker, petermad, Stefan2, white
- Ambaquista
- Junior Member
- Posts: 64
- Joined: 2005-05-27, 11:11 UTC
- Location: Luanda, Angola
Rename files tool x RegEx
I have several ebooks with filenames like:
title of the book - name of Author
I want to change the order of fields to:
Name of Author - title of the book
I´m sure I can do this with RegEx, but I don´t know how.
Any idea?
title of the book - name of Author
I want to change the order of fields to:
Name of Author - title of the book
I´m sure I can do this with RegEx, but I don´t know how.
Any idea?
-
- Junior Member
- Posts: 58
- Joined: 2016-01-15, 23:57 UTC
Re: Rename files tool x RegEx
Assuming both "name of author" and "title of the book" don't have the hyphen "-" in them (otherwise it can be messy), you can type the following into the rename window:
Search for:
Replace with:
I use .pdf as an example. You can do .mobi, .txt, or whatever your ebook format is.
Search for:
Code: Select all
(.+) - (.+).pdf
Code: Select all
$2 - $1.pdf
Re: Rename files tool x RegEx
2jiangzhenjerry
dot means any character..
and there may be text following pdf
dot means any character..
and there may be text following pdf
-
- Junior Member
- Posts: 58
- Joined: 2016-01-15, 23:57 UTC
Re: Rename files tool x RegEx
2white
Yes, I know both points you said. But it doesn't make much difference, because:
(1) It is a greedy search - it always tries to find the last "pdf", which lands in the file extension. Any other "pdf" in the filename will be skipped.
(2) "." does mean any character, but any character before the extension is dot itself ("\.") So it won't cause any issue.
I was just trying to make it as simple as possible for Ambaquista. Sometimes RegEx scares people away when we want to make it perfect.
Yes, I know both points you said. But it doesn't make much difference, because:
(1) It is a greedy search - it always tries to find the last "pdf", which lands in the file extension. Any other "pdf" in the filename will be skipped.
(2) "." does mean any character, but any character before the extension is dot itself ("\.") So it won't cause any issue.
I was just trying to make it as simple as possible for Ambaquista. Sometimes RegEx scares people away when we want to make it perfect.
Re: Rename files tool x RegEx
Assuming all files in the rename tool have extension .pdf . You didn't say so.jiangzhenjerry wrote: 2022-07-17, 17:51 UTC (1) It is a greedy search - it always tries to find the last "pdf", which lands in the file extension. Any other "pdf" in the filename will be skipped.
(2) "." does mean any character, but any character before the extension is dot itself ("\.") So it won't cause any issue.
Perhaps it is simpler to disable "Replace also in file extensions". Then the regex becomes even more simple and it works for all possible extensions.jiangzhenjerry wrote: 2022-07-17, 17:51 UTC I was just trying to make it as simple as possible for Ambaquista.
-
- Junior Member
- Posts: 58
- Joined: 2016-01-15, 23:57 UTC
Re: Rename files tool x RegEx
Well, I did say so: "I use .pdf as an example. You can do .mobi, .txt, or whatever your ebook format is."white wrote: 2022-07-17, 18:34 UTC Assuming all files in the rename tool have extension .pdf . You didn't say so.
Unless you are talking as if the user has a bunch of .pdf and .mobi files together in the same rename window and somehow the .mobi files have got "pdf" somewhere in their filenames... In that case he will just have to add a dollar sign "$" after "pdf".
I mean, you can (and I can, too) think of a hundred extreme cases if you want to break my answer and laugh at me. But I prefer showing people the beauty of Regex, rather than providing a long answer and demonstrating I'm Master Yoda. My years of Regex teaching experience told me it's important to make people love regex and not to push them away.
Yes, that's a good alternative, unless the user accidentally includes some .doc or .mp3 in the rename window that he doesn't intend to rename (yes, I'm just being sarcasticwhite wrote: 2022-07-17, 18:34 UTC Perhaps it is simpler to disable "Replace also in file extensions". Then the regex becomes even more simple and it works for all possible extensions.

- Ambaquista
- Junior Member
- Posts: 64
- Joined: 2005-05-27, 11:11 UTC
- Location: Luanda, Angola
Re: Rename files tool x RegEx
To: jiangzhenjerry and white
Thanks for the soluction and also for the conversation...
Works!
By the way, my "mediateca" is mainly epub (90% +), with some pdf, mht, etc
But, no problem, just change the extension.
Thanks again
Thanks for the soluction and also for the conversation...
Works!
By the way, my "mediateca" is mainly epub (90% +), with some pdf, mht, etc
But, no problem, just change the extension.
Thanks again
Re: Rename files tool x RegEx
Exactly, it could be a bunch of any kind of files.jiangzhenjerry wrote: 2022-07-17, 19:20 UTC Unless you are talking as if the user has a bunch of .pdf and .mobi files together in the same rename window and somehow the .mobi files have got "pdf" somewhere in their filenames...
Might as well add "\" in front of the "." too.jiangzhenjerry wrote: 2022-07-17, 19:20 UTC In that case he will just have to add a dollar sign "$" after "pdf".
Re: Rename files tool x RegEx
2Ambaquista
You're welcome.
BTW if you press F1 in the multi rename tool, you can look up regular expressions. It says:
You're welcome.
BTW if you press F1 in the multi rename tool, you can look up regular expressions. It says:
Help - Regular expressions wrote:
Subexpressions for search+replace
Text parts in round brackets are taken as subexpressions. Up to 89 subexpressions are supported now.
Example: To swap the title and interpret in the file name of an mp3 file, when they are separated by a dash (Title - Interpret.mp3), use the following options:
Search for: (.*) - (.*)\.mp3
Replace by: $2 - $1.mp3
Here $1 means the text in the first round bracket, and $2 the text in the second round bracket.
-
- Junior Member
- Posts: 58
- Joined: 2016-01-15, 23:57 UTC
Re: Rename files tool x RegEx
Once you add "$" then you don't need to add "\". You can if you like, but there is no need. I already explained it:white wrote: 2022-07-17, 20:48 UTCMight as well add "\" in front of the "." too.jiangzhenjerry wrote: 2022-07-17, 19:20 UTC In that case he will just have to add a dollar sign "$" after "pdf".
jiangzhenjerry wrote: (2) "." does mean any character, but any character before the extension is dot itself ("\.") So it won't cause any issue.
-
- Junior Member
- Posts: 58
- Joined: 2016-01-15, 23:57 UTC
Re: Rename files tool x RegEx
Not only a bunch of any kind of files BUT ALSO they have a different file extension mentioned in their filenames. How many times in your life have you encountered an .epub file that has "pdf" in its name? If that's indeed the case, a slightly loose rule could actually work better. For example, if a user converted a pdf to epub and has the filename aswhite wrote: 2022-07-17, 20:48 UTCExactly, it could be a bunch of any kind of files.jiangzhenjerry wrote: 2022-07-17, 19:20 UTC Unless you are talking as if the user has a bunch of .pdf and .mobi files together in the same rename window and somehow the .mobi files have got "pdf" somewhere in their filenames...
Code: Select all
Book - Aaron.pdf.epub
Code: Select all
Aaron - Book.pdf.epub
Code: Select all
Aaron.pdf - Book.epub
Code: Select all
^(.+) - (.+)\.pdf$
Re: Rename files tool x RegEx
Assuming there are no files with a name that ends with "<something>pdf". And leaving out the "\" probably results in people thinking .pdf in the regular expressions means .pdf, which is only true if it is prefixed with "\".jiangzhenjerry wrote: 2022-07-17, 21:05 UTC Once you add "$" then you don't need to add "\". You can if you like, but there is no need. I already explained it:
jiangzhenjerry wrote: (2) "." does mean any character, but any character before the extension is dot itself ("\.") So it won't cause any issue.
Why wouldn't there be "pdf" or "nopdfolder" or anything else containing "pdf" in any file name in any language?jiangzhenjerry wrote: 2022-07-17, 21:14 UTC Not only a bunch of any kind of files BUT ALSO they have a different file extension mentioned in their filenames. How many times in your life have you encountered an .epub file that has "pdf" in its name?
You can leave out the "^". Adding the two other characters doesn't add much to the complexity. Especially if you would add an explanation.jiangzhenjerry wrote: 2022-07-17, 21:14 UTC You can think of a thousand rare cases and write a complex regex string, but that's a waste of time if your real case doesn't hit those outliers. Suggesting an ultra-safe version like
works,but it is much less friendly for novice users and serves no purpose other than slightly bragging about my Regex skills.Code: Select all
^(.+) - (.+)\.pdf$