Rename files tool x RegEx

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
Ambaquista
Junior Member
Junior Member
Posts: 60
Joined: 2005-05-27, 11:11 UTC
Location: Luanda, Angola

Rename files tool x RegEx

Post by *Ambaquista »

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?
jiangzhenjerry
Junior Member
Junior Member
Posts: 58
Joined: 2016-01-15, 23:57 UTC

Re: Rename files tool x RegEx

Post by *jiangzhenjerry »

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:

Code: Select all

(.+) - (.+).pdf
Replace with:

Code: Select all

$2 - $1.pdf
I use .pdf as an example. You can do .mobi, .txt, or whatever your ebook format is.
User avatar
white
Power Member
Power Member
Posts: 4622
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Rename files tool x RegEx

Post by *white »

2jiangzhenjerry
dot means any character..
and there may be text following pdf
jiangzhenjerry
Junior Member
Junior Member
Posts: 58
Joined: 2016-01-15, 23:57 UTC

Re: Rename files tool x RegEx

Post by *jiangzhenjerry »

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.
User avatar
white
Power Member
Power Member
Posts: 4622
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Rename files tool x RegEx

Post by *white »

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.
Assuming all files in the rename tool have extension .pdf . You didn't say so.
jiangzhenjerry wrote: 2022-07-17, 17:51 UTC I was just trying to make it as simple as possible for Ambaquista.
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
Junior Member
Junior Member
Posts: 58
Joined: 2016-01-15, 23:57 UTC

Re: Rename files tool x RegEx

Post by *jiangzhenjerry »

white wrote: 2022-07-17, 18:34 UTC Assuming all files in the rename tool have extension .pdf . You didn't say so.
Well, I did say so: "I use .pdf as an example. You can do .mobi, .txt, or whatever your ebook format is."

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.
white 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.
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 sarcastic :twisted: )
User avatar
Ambaquista
Junior Member
Junior Member
Posts: 60
Joined: 2005-05-27, 11:11 UTC
Location: Luanda, Angola

Re: Rename files tool x RegEx

Post by *Ambaquista »

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
User avatar
white
Power Member
Power Member
Posts: 4622
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Rename files tool x RegEx

Post by *white »

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...
Exactly, it could be a bunch of any kind of files.
jiangzhenjerry wrote: 2022-07-17, 19:20 UTC In that case he will just have to add a dollar sign "$" after "pdf".
Might as well add "\" in front of the "." too.
User avatar
white
Power Member
Power Member
Posts: 4622
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Rename files tool x RegEx

Post by *white »

2Ambaquista
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.
jiangzhenjerry
Junior Member
Junior Member
Posts: 58
Joined: 2016-01-15, 23:57 UTC

Re: Rename files tool x RegEx

Post by *jiangzhenjerry »

white wrote: 2022-07-17, 20:48 UTC
jiangzhenjerry wrote: 2022-07-17, 19:20 UTC In that case he will just have to add a dollar sign "$" after "pdf".
Might as well add "\" in front of the "." too.
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.
jiangzhenjerry
Junior Member
Junior Member
Posts: 58
Joined: 2016-01-15, 23:57 UTC

Re: Rename files tool x RegEx

Post by *jiangzhenjerry »

white wrote: 2022-07-17, 20:48 UTC
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...
Exactly, it could be a bunch of any kind of files.
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 as

Code: Select all

Book - Aaron.pdf.epub
It is better to have the new filename as

Code: Select all

Aaron - Book.pdf.epub
rather than (if you disable "Replace also in file extensions" and remove file extensions in the search string)

Code: Select all

Aaron.pdf - Book.epub
It's all about what his real use case is. In practice you start with a minimum viable solution, and adjust it only when the simple solution doesn't fully address your specific situation. You can think of a thousand rare cases and write a complex rule, but that's a waste of time if your real case isn't one of those outliers. Suggesting a longer version like

Code: Select all

^(.+) - (.+)\.pdf$
works,but it is less friendly for a starter (what is ^, what is $, what is \ ...). It serves no purpose other than slightly showing off my skills to him. Why should I care? Maybe you enjoy showing off, but I don't.
User avatar
white
Power Member
Power Member
Posts: 4622
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Rename files tool x RegEx

Post by *white »

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.
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: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?
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 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

Code: Select all

^(.+) - (.+)\.pdf$
works,but it is much less friendly for novice users and serves no purpose other than slightly bragging about my Regex skills.
You can leave out the "^". Adding the two other characters doesn't add much to the complexity. Especially if you would add an explanation.
Post Reply