开发者

Renaming Folders using RegEx

开发者 https://www.devze.com 2023-01-06 23:52 出处:网络
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

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 = " ";

        Reg开发者_开发百科ex 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?


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);
    }
}


You need a Reverse Breadth First traversal to iterate over the directories.


You want DirectoryInfo's Name property. Once you have the directory, and the new name you can use the Directory.Move function to rename it.

If you want to drill down into a Directory's subdirectories you can use DirectoryInfo.GetDirectories method on the parent directory until that returns a null or empty array. The easiest way to do that is through recursion.

This function will allow you to drill down, you get it started with the parent directory:

    private void DrillDown(string folderPath) {
        DirectoryInfo info = new DirectoryInfo(folderPath);

        DirectoryInfo[] directories = info.GetDirectories();
        foreach(DirectoryInfo directory in directories) {
            fixFolder(directory.FullName);
        }
        renameFolder(folderPath);
    }

renameFolder is where you would rename the folder.

0

精彩评论

暂无评论...
验证码 换一张
取 消