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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:01:58+00:00 2026-05-15T17:01:58+00:00

PLEASE NOTE: This is a CODE QUESTION NOT AN SYSTEM MANIPULATION ONE. I have

  • 0

PLEASE NOTE: This is a CODE QUESTION NOT AN SYSTEM MANIPULATION ONE.

I have folders that have SEVERAL drilldowns (basically a folder inside a folder inside a folder…). I was wondering how if anybody knew how to rename folder names using RegEx? Basically i need to get rid of ~ ” # % & * : < > ? / \ { | } in any folder names. I was wondering how to accomplish this since i think i need to start from the “bottom” up (meaning the last possible drilldown folder and start renaming folders from the lowest level all the way up to the highest).

Anybody have any ideas?

So i have this code below:

 public partial class CleanPathResults : Form
{
    public CleanPathResults()
    {
        InitializeComponent();
    }

    public void Sanitizer(List<string> dirtyPaths)
    {
        string regPattern = (@"[~#&!%+{}]+");
        string replacement = " ";

        Regex regExPattern = new Regex(regPattern);
        Regex regExPattern2 = new Regex(@"\s{2,}");

        StreamWriter errors = new StreamWriter(@"S:\test\Errors.txt", true);


        var dirCount = new Dictionary<string, int>();


        dataGridView1.Rows.Clear();

           try
            {

              foreach (string invalidPaths in dirtyPaths)
              {
                string sanitizedPath = regExPattern.Replace(invalidPaths, replacement);
                  sanitizedPath = regExPattern2.Replace(sanitizedPath, replacement);


                        DataGridViewRow clean = new DataGridViewRow();
                        clean.CreateCells(dataGridView1);
                        clean.Cells[0].Value = invalidPaths;
                        clean.Cells[1].Value = sanitizedPath;

                        dataGridView1.Rows.Add(clean);
                        System.IO.Directory.Move(invalidPaths, sanitizedPath);  




               }
               }

           catch (Exception e)
           {
               throw;
               //errors.Write(e);


           }


    }

The biggest problem i’m facing here is that these paths need to be renamed from the lowest level folder to the highest level folder or I’ll keep getting errors thrown during debugging.

My question is how does one drill into a drive, go to the LOWEST folder in the treeview, rename folders upwards?
Take for instance the path: G:\Test~\This is only % a Test\test&test\testing!!\##test.txt

How would i possibly be able to rename this and start from the testing!! folder and work my way up so that i do not get any errors?

  • 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-15T17:01:59+00:00Added an answer on May 15, 2026 at 5:01 pm

    Your code seems to do a little too much. Here’s a snippet that just sanitizes the directory names.

    static void SanitizeFolders(DirectoryInfo root)
    {
        string regPattern = (@"[~#&!%+{}]+");
        string cleanName = "";
        Regex regExPattern = new Regex(regPattern);
    
        foreach (DirectoryInfo sub in root.GetDirectories())
        {
            if (regExPattern.IsMatch(sub.Name))
            {
                /* set cleanName appropriately... */
    
                sub.MoveTo(cleanName);
            }
        }
    
        //recurse into subdirectories
        foreach (DirectoryInfo sub in root.GetDirectories())
        {
            SanitizeFolders(sub);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 493k
  • Answers 493k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to use urlencode to encode the parameter data--something… May 16, 2026 at 10:37 am
  • Editorial Team
    Editorial Team added an answer Found it: Bug Changelog of fix Apparently fixed in 2.5.2.… May 16, 2026 at 10:37 am
  • Editorial Team
    Editorial Team added an answer I would use simple concatenation: Char.ToLowerInvariant(name[0]) + name.Substring(1) The first… May 16, 2026 at 10:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have my code here, it works fine from my home, where my user
I need to write some code to stress-test RS232 communication using rxtx ( note
OK, I'm still quite new to Xcode/Cocoa etc, so please forgive me if this
My code is having an XML parsing error at line 7 position 32 and
Below are three fragments of code, the first two are interfaces for two objects
I have a master-workers architecture where the number of workers is growing on a
I am struggling few hours with this and can't find a solution to the
I'm getting this error (see title) while trying to parse an XML file in
I'm trying to get a Wrapper from NHibernate with the following code: public Wrapper
I'm wondering how I can create a custom pop-up like the one in the

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.