tags:

views:

53

answers:

3

I have the following code:

public void DriveRecursion(string retPath)
    {
        string pattern = @"[~#&!%\+\{\}]+";

        Regex regEx = new Regex(pattern);

        string[] fileDrive = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);
        List<string> filePath = new List<string>();
        List<string> filePaths = new List<string>();


        dataGridView1.Rows.Clear();
        try
        {
            foreach (string fileNames in fileDrive)
            {
                SanitizeFileNames sw = new SanitizeFileNames();


                if (regEx.IsMatch(fileNames))
                {
                    string fileNameOnly = Path.GetFileName(fileNames);
                    string pathOnly = Path.GetDirectoryName(fileNames);

                    DataGridViewRow dgr = new DataGridViewRow();
                    filePath.Add(fileNames);
                    dgr.CreateCells(dataGridView1);
                    dgr.Cells[0].Value = pathOnly;
                    dgr.Cells[1].Value = fileNameOnly;
                    dataGridView1.Rows.Add(dgr);
                    //filePath.Add(fileNames);
                    filePaths.Add(fileNames);
                    paths.Add(fileNames);
                    //sw.FileCleanup(filePaths);

                }

                else
                {
                    continue;
                    //DataGridViewRow dgr2 = new DataGridViewRow();
                    //dgr2.Cells[0].Value = "No Files To Clean Up";
                    //dgr2.Cells[1].Value = "";
                }

            }

        }
        catch (Exception e)
        {
            StreamWriter sw = new StreamWriter(retPath + "ErrorLog.txt");
            sw.Write(e);

        }

    }

What i'm tryign to accomplish is that my application drills recursively into a drive/folder that the user specifies (through a FolderBrowserDialog) and goes through my if statement. IF the file contains any of the chars defined in my regex pattern, it gets output to my datagridview. If it does NOT, then do not show it on the datagridview.

For some reason right now, my code seems to pick up all files in a folder--not just specifically the ones with the chars in my RegEx pattern. I've been looking at this for quite some time and i'm not sure exactly why this is happening. Anybody have any ideas that perhaps I'm not catching?

+2  A: 

"\" will be treated as literals within your square brackets rather than escape characters. These are probably matching your file paths.

Try:

string pattern = @"[~#&!%+{}]+";
chrissr
+1  A: 

Yep you've used escape characters and specified the string is to be read literally by using the @ symbol

Basically @"cfnejbncie" means take the entire string literally. i.e you do not escape anything, it's like the whole string is escaped. So the / is actually being used as part of the regex.

Robert
Isn't that what I said? :D. I'm sure half the time describing the problem is half of it! I did indeed mean the \ was not escaping the following character in the regex, it's just a \ in the regex as a string.
Robert
+1  A: 

Hmmm. This works fine for me:

var regEx = new Regex(@"[~#&!%\+\{\}]+");
var files = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);

foreach (var fileName in files.Where(fileName => regEx.IsMatch(fileName)))
{
    Console.WriteLine(fileName);
}
Jim Lamb
Yup but he does state his code picks up all the files and not only those with the special characters in them.
Robert
@Robert, I know but the above code does not pick up all files - only those with the special characters in them.
Jim Lamb
is it perhaps b/c of this line that it works for you? foreach (var fileName in files.Where(fileName => regEx.IsMatch(fileName)))How would i implement this in my code? I'm unfamiliar with Where...