Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:33:20+00:00 2026-06-16T02:33:20+00:00

I am looking to write a utility to batch rename a bunch of files

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:33:22+00:00Added an answer on June 16, 2026 at 2:33 am

    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.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace renamer
    {
        class RenameImpl
        {
            public static IEnumerable<Tuple<string,string>> RenameWithPatterns(
                string path, string curpattern, string newpattern,
                bool caseSensitive)
            {
                var placeholderNames = new List<string>();
    
                // Extract all the cur_placeholders from the user's input pattern
                var input_regex = new Regex(@"(\%[^%]+\%)");
                var cur_matches = input_regex.Matches(curpattern);
                var new_matches = input_regex.Matches(newpattern);
                var regex_pattern = new StringBuilder();
    
                if (!caseSensitive)
                    regex_pattern.Append("(?i)");
                regex_pattern.Append('^');
    
                // Do a pass over the matches and grab info about each capture
                var cur_placeholders = new List<Tuple<string, int, int>>();
                var new_placeholders = new List<Tuple<string, int, int>>();
                for (var i = 0; i < cur_matches.Count; ++i)
                {
                    var m = cur_matches[i];
                    cur_placeholders.Add(new Tuple<string, int, int>(
                        m.Value, m.Index, m.Length));
                }
                for (var i = 0; i < new_matches.Count; ++i)
                {
                    var m = new_matches[i];
                    new_placeholders.Add(new Tuple<string, int, int>(
                        m.Value, m.Index, m.Length));
                }
    
                // Build the regular expression
                for (var i = 0; i < cur_placeholders.Count; ++i)
                {
                    var ph = cur_placeholders[i];
    
                    // Get the literal before the first capture if it is the first
                    if (i == 0 && ph.Item2 > 0)
                        regex_pattern.Append(Regex.Escape(
                            curpattern.Substring(0, ph.Item2)));
    
                    // Generate the capture for the placeholder
                    regex_pattern.AppendFormat("(?<{0}>.*?)",
                        ph.Item1.Replace("%", ""));
    
                    // The literal after the placeholder
                    if (i + 1 == cur_placeholders.Count)
                        regex_pattern.Append(Regex.Escape(
                            curpattern.Substring(ph.Item2 + ph.Item3)));
                    else
                        regex_pattern.Append(Regex.Escape(
                            curpattern.Substring(ph.Item2 + ph.Item3,
                            cur_placeholders[i + 1].Item2 - (ph.Item2 + ph.Item3))));
                }
    
                regex_pattern.Append('$');
    
                var re = new Regex(regex_pattern.ToString());
    
                foreach (var pathname in Directory.EnumerateFileSystemEntries(path))
                {
                    var file = Path.GetFileName(pathname);
                    var m = re.Match(file);
    
                    if (!m.Success)
                        continue;
    
                    // New name is initially same as target pattern 
                    var newname = newpattern;
    
                    // Iterate through the placeholder names
                    for (var i = new_placeholders.Count; i > 0; --i)
                    {
                        // Target placeholder name
                        var tn = new_placeholders[i-1].Item1.Replace("%", "");
    
                        // Get captured value for this capture
                        var ct = m.Groups[tn].Value;
    
                        // Perform the replacement
                        newname = newname.Remove(new_placeholders[i - 1].Item2,
                            new_placeholders[i - 1].Item3);
                        newname = newname.Insert(new_placeholders[i - 1].Item2, ct);
                    }
    
                    newname = Path.Combine(path, newname);
                    yield return new Tuple<string, string>(pathname, newname);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a utility that will allow moving files in Windows,
I am looking to write a simple and easy method that will allow me
I'm looking to write a portable filesystem scanner, capable of listing all files on
I'm looking to write some C# that will detect a piece of a URL
I'm looking to write a jQuery function that will change the value of other
I'm looking to write a script that will load up a file that has
I'm looking to write a plugin for IE that will do a little parsing
I am looking to write a utility for work where I can take a
I'm looking to write a Java program which will download a Java source file
I am looking to write application for iPhone which will be able to control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.