tags:

views:

30

answers:

1

I have an app that "cleans" "dirty" filenames. "Dirty" filenames have #%&~+{} in their filenames. What my app does is see if they are a match for a RegEx pattern i have defined and then send it to a method called FileCleanUp where it "cleans" the file and replaces invalid chars with a "". However, i noticed while i was running this, that my FileCleanup method only works on SOME files and not others!

Here is my code:

    public class SanitizeFileNames
{

    public void FileCleanup(List<string>paths)
    {
        string regPattern = (@"[~#&!%+{}]+");
        string replacement = "";
        Regex regExPattern = new Regex(regPattern);


        foreach (string files2 in paths)
            try
            {
                string filenameOnly = Path.GetFileName(files2);
                string pathOnly = Path.GetDirectoryName(files2);
                string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
                string sanitized = Path.Combine(pathOnly, sanitizedFileName);
                //write to streamwriter
                System.IO.File.Move(files2, sanitized);
            }
            catch (Exception e)
            { 
            //write to streamwriter

            }

            }

I tested on a few files with the names like: ~Test.txt, #Test.txt, +Text.txt, Test&Test.txt, T{e}st.txt, Test%.txt.

The ones i could not get to be renamed were: ~Test.txt, +Test.txt, T{e}st.txt

I debugged this and weirdly enough, it shows that these files that did not get renamed for some reason as correct on the debugger. Instead of showing ~Test.txt as a "sanitized" file name, it was Text.txt. So on the app side, it DOES read my foreach loop correctly.

However, i'm really stumped as to why it's not actually renaming these files. Anybody have a clue as to why this might be? Does it have to do with the File.Move() ?

EDIT: On further testing, i realize that it also doesn't rename files that are like this: ~~test.txt or ##test.txt

+1  A: 

You have an empty catch block. Why don't you print out the exception message to see what's happening?

My guess is that the file names are matching the regex, but you're unable to rename the files because a file named Test.txt already exists after #Test.txt is renamed to that. Try renaming the other files to ~Test2.txt, +Test3.txt, T{e}st4.txt before running the program again.

Bill the Lizard
ha! how simple i didn't even think of that :) thank you much sir!
Bill the Lizard
any idea how i could capture the fact that a file couldn't be renamed b/c of a duplicate that already existed in the folder in my Catch block? i'm new to C# and error logging is still also a new concept to me.
@yeahumok: You'd have to look to see exactly what exception is being thrown. I'd probably try to handle it before the catch block is reached though, by checking to see if `sanitizedFileName` already exists before you write to the stream.
Bill the Lizard