I am trying to rename files that my program has as “illegal characters” for importing SharePoint files. The illegal characters that I mean are: ~ #% and * {} / \ |: <> - ""
What I'm trying to do is overwrite the disc, collect a list of file names, and then through regular expressions, select the file names from the list and try to replace the invalid characters in the files themselves.
Does anyone know how to do this? So far I have this: (remember, I am full n00b for this stuff)
class Program
{
static void Main(string[] args)
{
string[] files = Directory.GetFiles(@"C:\Documents and Settings\bob.smith\Desktop\~Test Folder for [SharePoint] %testing", "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
Console.Write(file + "\r\n");
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
string pattern = " *[\\~#%&*{}/:<>?|\"-]+ *";
string replacement = " ";
Regex regEx = new Regex(pattern);
string[] fileDrive = Directory.GetFiles(@"C:\Documents and Settings\bob.smith\Desktop\~Test Folder for [SharePoint] %testing", "*.*", SearchOption.AllDirectories);
StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\bob.smith\Desktop\~Test Folder for [SharePoint] %testing\File_Renames.txt");
foreach(string fileNames in fileDrive)
{
string sanitized = regEx.Replace(fileNames, replacement);
sw.Write(sanitized + "\r\n");
}
sw.Close();
}
}
So I need to figure out how to recursively look for these invalid characters, replace them in the file name itself. Does anyone have any ideas?