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 485k
  • Answers 485k
  • 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 An example of writing t to all files: for x… May 16, 2026 at 7:37 am
  • Editorial Team
    Editorial Team added an answer Just replace: $query = "$item->".$param; by $query = $item->$param; Hope… May 16, 2026 at 7:37 am
  • Editorial Team
    Editorial Team added an answer Looks like no one is willing to answer your question.… May 16, 2026 at 7: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

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.