I am looking to write a utility to batch rename a bunch of files at once using a regular expression. The files that I will be renaming all at once follow a certain naming convention, and I want to alter them to a new naming convention using data that’s already in the filenames; but not all my files follow the same convention currently.
So I want to be able to write a general use program that lets me input into a textbox during runtime the pattern of the filename, and what tokens I want to extract from the filename to use for renaming.
For example – Assume I have one file named [Coalgirls]_Suite_Precure_02_(1280x720_Blu-Ray_FLAC)_[33D74D55].mkv. I want to be able to rename this file to Suite Precure - Ep 02 [Coalgirls][33D74D55].mkv
This means I would preferably be able to enter into my program before renaming something akin to [%group%]_Suite_Precure_%ep%_(...)_[%crc%].mkv and it would populate the local variables group, ep, and crc to use in the batch rename.
One particular program I’m thinking of that does this is mp3tag, used for converting file names to id3 tags. It lets you put something like %artist% – %album% – %tracknumber% – %title%, and it takes those 4 tokens and puts them into the respective id3 tags.
How can I make a system similar to this without having to make the user know regex syntax?
As mentioned by usr, you can extract all the named placeholders in the search string using
%(?<name>[^%]+)%. This will get you “group”, “ep”, and “crc”.Now you need to scan all the fragments between the placeholders and put a capture at each placeholder in the regex. I’d iterate through the matches from above (you can get start offset and length of each match to navigate through the non-placeholder fragments).
(There are mistakes in your example, I’ll assume the last part is correct and I’m dropping the mysterious (…))
It would build a regex that looks like this:
^%(?<group>.*?)_Suite_Precure_(?<ep>.*?)_(?<crc>.*?).mkv$Pass the literal fragments to Regex.Escape before using it in the regex to handle troublesome characters properly.
Now, for each filename, you try to match the regex to it. If it matches, you get the values of the placeholders for this file. Then you take those placeholder values and merge them into the output pattern, replacing the placeholders appropriately. This gives you the new name, you can do the rename.