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

  • Home
  • SEARCH
  • 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 7778641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:26:56+00:00 2026-06-01T18:26:56+00:00

What I want to do is rename all file in a particular folder, such

  • 0

What I want to do is rename all file in a particular folder, such that if a filename contains any digit in it, it is removed.

say, if a filename is
someFileName.someExtension it remains the same, but if a file is like this,
03 - Rocketman Elton John it should be renamed to Rocketman Elton John (I did the part to remove the -), another example, if the filename is 15-Trey Songz - Unfortunate (Prod. by Noah 40 Shebib) it should be renamed to Trey Songz Unfortunate (Prod. by Noah Shebib) (again I can remove -). The user is asked to select the folder like this

private void txtFolder_MouseDown(object sender, MouseEventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            fd.RootFolder = Environment.SpecialFolder.Desktop;
            fd.ShowNewFolderButton = true;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = fd.SelectedPath;
            }
        }

Also, it renaming starts like this

private void btnGo_Click(object sender, EventArgs e)
{
    StartRenaming(txtFolder.Text);
}

and

private void StartRenaming(string FolderName)
{
   string[] files = Directory.GetFiles(FolderName);
   foreach (string file in files)
        RenameFile(file);
}

Now in rename file, I need the function, the regular expression that will remove any number(s) in file.
Its is implemented as

private void RenameFile(string FileName)
{
    string fileName = Path.GetFileNameWithoutExtension(FileName);
   /* here the function goes that will find numbers in filename using regular experssion and replace them */ 
}

so what I can do is, I can use something like

1  var matches = Regex.Matches(fileName, @"\d+");
2  
3  if (matches.Count == 0)
4    return null;
5
6  // start the loop
7  foreach(var match in matches)
8  {
9     fileName = fileName.Replace(match, ""); /* or fileName.Replace(match.ToString(), ""), whatever be the case */    
10 }
11  File.Move(FileName, Path.Combine(Path.GetDirectoryName(FileName), fileName));
12  return;

But I don’t think that’s the right way to do it? Is there any better option to do this? or is this the best (and only option) to do this? Also, is there anything like IN in String.Replace? Say in sql I can use IN in a select command and specify a bunch of where conditions, but is there something like this with String.Replace so that I don’t have to run the loop I ran from line 7 to 10? Are there any other better options?

ps: about that regex, I posted a question Regular Expression for numbers? (apparently I wasn’t clear enough) and from that I got my regex, if you think someother regex would do better please tell me, also if you need any other information please let me know…

  • 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-01T18:26:58+00:00Added an answer on June 1, 2026 at 6:26 pm

    For replacing you should look into Regex.Replace which can replace all occurences at once.

    Otherwise code look ok (with exception of strange fileName.Replace("match", "") which uses constant string…)

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

Sidebar

Related Questions

I have a file in a different folder I want to rename in perl,
I want to rename all the files in a folder which starts with 123_xxx.txt
I'm writing a file sharing hosting and I want to rename all the files
I want to rename a subscription database in merge replication. All the subscriptions are
I want to rename my project. For that, I have to just choose project
I want to rename an context private file created with openFileOutput() but I don't
I have a lot of images (.png) file. Now i want to rename the
I have a working file rename java tool, now I want to add an
I want to rename file and upload it into server as well as in
In PHP I want to rename (move/copy) a file on a windows file server:

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.