A Handy Way to Use a Regex to Rearrange Files - Plus Quick Intro to Renaming

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

A Handy Way to Use a Regex to Rearrange Files - Plus Quick Intro to Renaming

Post by Dustydog »

I love move on rename, so I thought I'd share this fairly trivial, yet handy, example:

If you've normalized your files in a common way such as: "creator - work and whatever else"
simply plop them into a folder, select all, right-click -> Rename Special -> RegExp Rename:

((.+?)(?:\s-\s)(?:.+)) > E:\Sorted\$2\$1

You end up with all the works arranged by creator in the folder "E:\Sorted."

***
Simple notes on the regex:

The space > space is just XY's regex match > replace syntax. If you want to replace with nothing, still put a space after the ">".

A dot "." means match anything once (basically); a "+" afterwards means match at least one of the previous (here any single character, the dot) as many times as possible - i.e. until it bumps into something you've put next and it has to give back part of what it's already matched - just flow with that if you don't know what I mean, quite.

$1 (or whatever number) is a capture group, counting from left to right. In the case of the above, the farthest left includes everything, the next includes "creator" - parentheses are groupings that work about like you'd think they do.

?: creates a non-capturing group so there are no excess capture-group numbers involved to keep track of - plus it's a habit; it isn't necessary here, but if a regex gets long (XY doesn't have named groups - you have to use numbers) - it helps to keep track. Plus it helps to not run out of numbers for groups in a very complex regex.

\s is just a space that's easier to see.

The first question mark +? is necessary in case you have a file like "creator - work - and whatever else.mp3" so that it doesn't capture to the second " - " (it makes $2 non-greedy so that it stops at the first \s-\s).

So: It captures the creator in the $2 group (remember $1 is leftmost) up to the " - ". The whole thing is captured in group $1 so you keep the file name intact and simply specify the E:\directory\creator\FileOrFolderName where you're putting it. (You can use this on either folders or files as extension is included if present, but I've only used it with folders myself.) You won't get any duplicates because you've got everything in the same folder in the first place.

(I merge later to my main storage folder and handle dups then and do things in multiple batches either as they arise or to keep the quantity of files reasonable.)

***

Regular Expressions are infinitely worth knowing.

Just Great Software run by Jan Goyvaerts has some excellent tutorial information on regular expressions in general, as does a site called Rexegg, run by a Goyvaerts fan and enthusiastic expert. They have links to other resources.

Side Note:
XY may not have the most feature-rich regex flavor (Such as named groups. It's Visual Basic 5 - though I don't believe that's significantly different from 6; someone correct me if I'm wrong and in what way.), but it's incredibly reliable. I haven't ever had it crash on me, unlike some more feature-rich tools, and there are lovely previews. I try to use XY for all my renaming whenever possible, though I myself have other tools available.

***

And if you haven't learned the sheer beauty of XY's Batch Rename - you should. Example: *\* in Batch Rename puts a file in a folder of the same name, minus the extension - with three characters! And the numbering system is easy as can be "<#01>. *" pads to two digits, adds a period space, and numbers all the selected files starting with 01. while including the entire filename. Can't be much simpler than that. If you wish, you could change the filename to whatever you want "<#01>. Something Better" - glance at the "i" for some quick, simple syntax information.

Plus he gives you search and replace renames (often the most direct way to solve a small problem) and several handy right-click, quick-rename options. I find myself using most of these in combination.

And remember: You can also Find with a regex. For instance, get files that aren't normalized by entering, perhaps ".+ - .+" as your Find query after enabling a regex search (you may want to limit to no subfolders), then check the inverse box to find outliers to hand edit.

I highly recommend you start a file of useful regular expressions to use in XY for quick reference.

***

Thanks for your great work, Don. Your renaming is truly excellent. I've got other tools, but nothing does as well as XY for reliability, number of options, and for simply being right there quickly.

[Edited for completeness.]
Last edited by Dustydog on 13 Jan 2017 00:40, edited 4 times in total.

Jerry
Posts: 835
Joined: 05 May 2010 15:48
Location: The UnUnited States of America

Re: A Handy Way to Use a Regex to Rearrange Files - Plus Quick Intro to Renaming

Post by Jerry »

Agreed! The regular expression rename capability in XYplorer is one of the top power features for me. I do this all the time, in various configurations. Normalizing folder/file naming and especially, inclusion of key descriptive data in the names, allows me to use the Everything search program to instantaneously find anything by name across dozens of terabytes on multiple drives, and makes it easier to look through the results when sorted.
Running on Windows 10 Pro 64-bit quad-core ASUS G752-VY notebook with 64 GB RAM, over 26 external USB3 drives attached via multiple powered hubs with letters and mount points, totaling 120+ TB. SCREEN SCALING: 125%

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

Re: A Handy Way to Use a Regex to Rearrange Files - Plus Quick Intro to Renaming

Post by Dustydog »

Jerry wrote:Agreed! The regular expression rename capability in XYplorer is one of the top power features for me. I do this all the time, in various configurations. Normalizing folder/file naming and especially, inclusion of key descriptive data in the names, allows me to use the Everything search program to instantaneously find anything by name across dozens of terabytes on multiple drives, and makes it easier to look through the results when sorted.
"Everything" by voidtools - free, no strings at all, donation ware, a must-have - is also one of my favorite tools. XY's Find is incredibly powerful, but Everything can find, well, everything instantly, including on network shares if you ask it to, or results from offline drives. You wouldn't want to query every filename you have in XY over many large drives and network shares very often! With Everything, you can.

I not-infrequently use it in combination with XY's paper folders. It makes a great adjunct to XY.

Its forte is finding filenames, though you can find some things in contents (if you limit things to a reasonable number of files). Do install the 1.4 Beta! You can also do little tricks like you can with XY, albeit with a different syntax [readme.txt /p //find all folders that contain a file readme.txt - so you're using a parent command instead of child: like in Everything], like find all folders with a child that's a .jpg or whatever. It supports multiple query types, as does XY, including, as you mentioned - regular expressions. You can, like you can with XY, also look along an entire path to help find things you haven't normalized yet and you can't recall quite where you put them - it gives results as fast as you can type, so you can try many queries.

It does its one thing, find filenames, exceedingly well - you won't want to go without it, even if you don't have a huge number of files. It creates its NTFS indices incredibly quickly, the indices are small, and it isn't awful with other drives for indexing speed even when it can't take advantage of its NTFS trickiness while indexing. (It has a very deep feature set for its One Big Thing - do yourself a favor and look at the site.)

It's developed by one very bright guy - sound familiar? If you find it useful, toss him a donation when you can, and he won't even nag you about it. My favorite utilities all happen to be single-developer: Jan Goyvaert's regex tools (including his very nice Notepad program - there's an excellent free version - its got a very nice feature set, especially the regex engine), Everything, and most of all - XY.

Nice mention! I hope more people discover it!
Last edited by Dustydog on 13 Jan 2017 00:32, edited 1 time in total.

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

Re: A Handy Way to Use a Regex to Rearrange Files - Plus Quick Intro to Renaming

Post by Dustydog »

Got off-topic talking about PowerGREP, so moved it, appropriately, to Other Software:

viewtopic.php?f=11&t=16888

Post Reply