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 955423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:22:42+00:00 2026-05-16T00:22:42+00:00

I don’t have much experience with regexes and I wanted to rectify that. I

  • 0

I don’t have much experience with regexes and I wanted to rectify that. I decided to build an application that takes a directory name, scans all files (that all have a increasing serial number but differ subtly in their filenames. Example : episode01.mp4, episode_02.mp4, episod03.mp4, episode04.rmvb etc.)

The application should scan the directory, find the number in each file name and rename the file along wit the extension to a common format (episode01.mp4,episode02.mp4,episode03.mp4,episode04.rmvb etc.).

I have the following code:

Dictionary<string, string> renameDictionary = new Dictionary<string,string>();
DirectoryInfo dInfo = new DirectoryInfo(path);
string newFormat = "Episode{0}.{1}";
Regex regex = new Regex(@".*?(?<no>\d+).*?\.(?<ext>.*)"); //look for a number(before .) aext: *(d+)*.*
foreach (var file in dInfo.GetFiles())
{
  string fileName = file.Name;
  var match = regex.Match(fileName);
  if (match != null)
  {
    GroupCollection gc = match.Groups;
    //Console.WriteLine("Number : {0}, Extension : {2} found  in {1}.", gc["no"], fileName,gc["ext"]);
    renameDictionary[fileName] = string.Format(newFormat, gc["no"], gc["ext"]);
  }
}
foreach (var renamePair in renameDictionary)
{
  Console.WriteLine("{0} will be renamed to {1}.", renamePair.Key, renamePair.Value);
  //stuff for renaming here
}

One problem in this code is that it also includes files which don’t have numbers in the renameDictionary. It would also be helpful if you could point out any other gotchas that I should be careful about.

PS: I am assuming that the filenames will only contain numbers corresponding to serial (nothing like cam7_0001.jpg)

  • 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-05-16T00:22:43+00:00Added an answer on May 16, 2026 at 12:22 am

    This simplest solution is probably to use Path.GetFileNameWithoutExtension to get the file name, and then the regex \d+$ to get the number at its end (or Path.GetExtension and \d+ to get the number anywhere).

    You can also achieve this in a single replace:

    Regex.Replace(fileName, @".*?(\d+).*(\.[^.]+)$", "Episode$1$2")
    

    This regex is a bit better, in that it forces the extension not to contain dots.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much PHP experience and I want to know how to best
Don't have much to say, just can get into the event handler. XAML: <Grid>
DON'T ASK WHY but... I have a regex that needs to be case insensitive
I don't have any experience in updating a Rails app and when I google
Don't yell at me! i have seen many threads claiming that HTML cannot be
I don't want to know a way to preload images, I found much on
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Don't think that I'm mad, I understand how php works! That being said. I
Don't be frightened, its a very basic code. Just wanted to check with you
Don't they both have to convert to machine code at some point to execute

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.