プログラムがSharePointファイルのインポートで「不正な文字」としてリストしているファイルの名前を変更しようとしています。私が参照している不正な文字は次のとおりです。〜#%&* {} / \ | :<>?-""
私がやろうとしているのは、ドライブを再帰的に実行し、ファイル名のリストを収集してから、正規表現を使用して、リストからファイル名を選択し、実際のファイル名自体の無効な文字を置き換えてみることです。
誰かがこれを行う方法を知っていますか?これまでのところ私はこれを持っています:(覚えておいてください、私はこのようなものの完全なn00bです)
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();
}
}
したがって、私が理解する必要があるのは、これらの無効な文字を再帰的に検索し、実際のファイル名自体に置き換える方法です。誰かアイデアはありますか?