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?