views:

56

answers:

2

Hello

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  A: 

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 :\*.

Humberto
Do i need to put an if/else statement in here? Or if the file has no invalid characters, it'll automatically skip it?
@yeahumok, you may indeed compare `fileNames` with `sanitized` to avoid an unnecessary call to `File.Move()`. However, pay attention to duplicates (like, `#File.txt` and `~File.txt` will be named `File.txt`), and check for access permissions, disk space, and other IO exceptions.
Humberto
+1  A: 

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.

slf
i took a look at the custom iterator link. where do i pass in the directory i want to drill into?
`GetFilesRecursive(dirInfo);` -- just pass a root dirInfo
slf