问题
I'm trying to rename files that my program lists as having "illegal characters" for a SharePoint file importation. The illegal characters I am referring to are: ~ # % & * {} / \ | : <> ? - ""
What i'm trying to do is recurse through the drive, gather up a list of filenames and then through Regular Expressions, pick out file names from a List and try to replace the invalid characters in the actual filenames themselves.
Anybody have any idea how to do this? So far i have this: (please remember, i'm a complete n00b to 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 what i need to figure out is how to recursively search for these invalid chars, replace them in the actual filename itself. Anybody have any ideas?
回答1:
When you are working recursively with files and directories, many times it's easier to use the DirectoryInfo class and it's members instead of the static methods. There is a pre-built tree structure for you, so you don't have to manage that yourself.
GetDirectories returns more DirectoryInfo instances so you can walk the tree, while GetFiles returns FileInfo objects.
This guy created a custom iterator to recursively yield file info, which when you combine it with your existing regex work, would complete your solution.
回答2:
File.Move() effectively renames files. Basically, you'll just need to
File.Move(fileNames, sanitized);
inside the latter loop.
ALERT - possibly there'll be duplicate file names, so you'll have to establish a policy to avoid this, like appending a counter at the end of the sanitized
variable. Also, apply a proper exception handling.
PS: Certainly, you don't need to search for characters like :\*
.
来源:https://stackoverflow.com/questions/3015965/changing-filenames-using-regex-and-recursion