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 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

Related Questions

Please note: This is a question about the Eclipse plugin Subversive , and not
Please note this is not a question about online/hosted SVN services. I am working
[Please note that this is a different question from the already answered How to
Please note that this is not homework and i did search before starting this
Please note that this question is from 2008 and now is of only historic
Please note this is a question with regard to Backbone.js. This is not a
Question Please note that the solution to this is directly below using Eugen's view
Please note that this question is about CGLayer (which you typically use to draw
Please note this application will never be running on a server system. I am
Please note this question related to performance only. Lets skip design guidelines, philosophy, compatibility,

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.