我正在尝试重命名我的程序列出的具有“非法字符”的文件以用于 SharePoint 文件导入。我指的非法字符是: ~ # % & * {} / \ | :<>?- “”
我要做的是通过驱动器递归,收集文件名列表,然后通过正则表达式,从列表中挑选文件名并尝试替换实际文件名本身中的无效字符。
有人知道怎么做吗?到目前为止我有这个:(请记住,我对这个东西完全不了解)
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();
}
}
所以我需要弄清楚的是如何递归搜索这些无效字符,将它们替换为实际文件名本身。有人有什么想法吗?