Put File in Folder with the Same Base Name in One Step

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Dustydog
Posts: 321
Joined: 13 Jun 2016 04:19

Put File in Folder with the Same Base Name in One Step

Post by Dustydog »

This might seem kind of trivial, but it's a little script I use all the time - sometimes just interactively with a regular expression rename.

All it does is put a file into a folder with the same base name as the file. Something I do fairly often, for example if I want to put a cover image and a description file in with some media. It's also handy because if you name something with the same base and different extensions - for example a DVD with an .srt file - it puts it into the same folder. I just tweaked it a little to give the same results as gpc, i.e. no dot when you get the extension.

You can, of course, select multiple files (and even folders?!) at once, and it puts them all into the correct folder depending on their base name.

The only reason I'm sharing it is because getting the regex right was a little tricky, and I thought others might find it useful.

Code: Select all

rename r, "^((?:(?:.+)(?=\.)(?!\.[ ]))|(?:.+))(?:\.(.+))? > $1\$1.$2", "p";
/* Captures from the beginning everything up to the last dot using a positive lookahead to skip intervening dots, 
or captures everything if there is no dot, into $1. Using a negative lookahead, this skips identifying anything as an extension
that has a dot followed by a space. $2 is an optional extension. 
This works fine on files or folders: $1 is the base, $2 is the extension.
*/
Of course, if you script it, you could do it with GetPathComponent, aka gpc, but it's handy this way.
Last edited by Dustydog on 26 Nov 2019 22:20, edited 1 time in total.

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Put File in Folder with the Same Base Name in One Step

Post by highend »

May I ask why rename "b", "*\*", "p"; would be not as good as using a (very complicated) regex?
Don't get me wrong, I'm always a friend of a good regex but seriously, somebody needs to be a pro
to understand this one (without a software that explains it in every detail...) :tup:
One of my scripts helped you out? Please donate via Paypal

Dustydog
Posts: 321
Joined: 13 Jun 2016 04:19

Re: Put File in Folder with the Same Base Name in One Step

Post by Dustydog »

Because I use it elsewhere and tweak it.

admin
Site Admin
Posts: 60558
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Put File in Folder with the Same Base Name in One Step

Post by admin »

highend wrote: 26 Nov 2019 22:18 ... rename "b", "*\*", "p"; ...
Cool way. :tup:

Dustydog
Posts: 321
Joined: 13 Jun 2016 04:19

Re: Put File in Folder with the Same Base Name in One Step

Post by Dustydog »

It also happened to just come to my attention today because I discovered there was a bug when move on rename tries to create a directory and put a file in it if the file ends up being long path - either using a regex or a batch rename (like the one you just showed).

Plus, I thought it was an interesting regular expression because it uses both a positive lookahead and a negative lookahead as well as an OR that keeps everything in the right capture group.

The most ridiculously long regex I actually use is this one. What it does is get various pieces of my Standard Audio Folder naming scheme. It looks insane, but it's simple to reuse, which is something I've found with most regexes - once you get one right, they're fairly easy to tweak, and if the capture groups are right, they're useful for doing things like pulling pieces out of something complicated like my personal naming scheme (which is quite complicated) or confirming that something conforms to, in this case, my naming scheme. All I need to do with this one is change which capturing group I retrieve, or use a regexmatches. I don't need to recode anything for these purposes ever, and I can do them in one line. And yes, to get something like this bug free, I certainly used RegexBuddy - wonderful utility. (This one has a few more groups than necessary ?:'ed out in case I just want the content, or if I want the content plus its padding, such as a ' - text' vs 'text' or (2001) vs 2001.)

Code: Select all

^((?:[^-]+);\s)?((?:.+?)\s-\s)((?:(?!\s\[)(?!\s-\s)(?!\s\(\d{4}\)).)+)(\s-\s(?:[^\(\[]+)\b)?(\s?\((?:[\s\d,-]+)\))?(\s?\[(?:[+\s,\w\d-]+)\])

admin
Site Admin
Posts: 60558
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Put File in Folder with the Same Base Name in One Step

Post by admin »

The human, and certainly my, brain is not made for this.

Dustydog
Posts: 321
Joined: 13 Jun 2016 04:19

Re: Put File in Folder with the Same Base Name in One Step

Post by Dustydog »

I also wrote it because if I have a file like whatever.eng.srt and whatever.mp4 and want to put them in the same folder with one quick command, doing it with a batch cuts it off wrong and puts them into whatever.eng\whatever.eng.srt AND whatever\whatever.mp4, whereas the regex puts both in whatever\whatever.eng.srt along with whatever\whatever.mp4.

Dustydog
Posts: 321
Joined: 13 Jun 2016 04:19

Re: Put File in Folder with the Same Base Name in One Step

Post by Dustydog »

lol, it looks much better commented and spread out.

admin
Site Admin
Posts: 60558
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Put File in Folder with the Same Base Name in One Step

Post by admin »

Dustydog wrote: 26 Nov 2019 22:45 It also happened to just come to my attention today because I discovered there was a bug when move on rename tries to create a directory and put a file in it if the file ends up being long path - either using a regex or a batch rename (like the one you just showed).
Confirmed and fixed, thanks! :tup:

Post Reply