tags:

views:

125

answers:

4

I'm having a bit of trouble passing this parameter to a class i have. Does anybody have any ideas?

Class 1's code:

public void DriveRecursion(string retPath)
{
    //recurse through files.  Let user press 'ok' to move onto next step        
    // string[] files = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);

    string pattern = " *[\\~#%&*{}/<>?|\"-]+ *";
    //string replacement = "";
    Regex regEx = new Regex(pattern);

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



    dataGridView1.Rows.Clear();
    try
    {
        foreach (string fileNames in fileDrive)
        {

            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);

                 \\I want to pass fileNames to my FileCleanup Method
                 \\I tried this:
               \\SanitizeFileNames sf = new SanitizeFileNames();
               \\sf.Add(fileNames); <-- this always gets an error..plus it is not an action i could find in intellisense


            }

            else
            {
                continue;
            }

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

    }
}

Class 2's code:

public class SanitizeFileNames
{

    public void FileCleanup(string fileNames)
    {
        string regPattern = " *[\\~#%&*{}/<>?|\"-]+ *";
        string replacement = "";
        Regex regExPattern = new Regex(regPattern);
    }

What i want to do in SanitizeFileNames is do a foreach through the FileNames & FilePath and replace invalid chars (as defined in my Regex pattern). So, something along the lines of this:

using (StreamWriter sw = new StreamWriter(@"S:\File_Renames.txt"))
{
    //Sanitize and remove invalid chars  
    foreach (string Files2 in filePath)
    {
        try
        {
            string filenameOnly = Path.GetFileName(Files2);
            string pathOnly = Path.GetDirectoryName(Files2);
            string sanitizedFilename = regEx.Replace(filenameOnly, replacement);
            string sanitized = Path.Combine(pathOnly, sanitizedFilename);
            sw.Write(sanitized + "\r\n");
            System.IO.File.Move(Files2, sanitized);
        }
        //error logging
        catch(Exception ex)
        {
            StreamWriter sw2 = new StreamWriter(@"S:\Error_Log.txt");
            sw2.Write("ERROR LOG");
            sw2.WriteLine(DateTime.Now.ToString() + ex + "\r\n");
            sw2.Flush();
            sw2.Close();
        }
    }
}

However, I'm having trouble passing the fileNames into my SanitizeFileNames class. Can anybody help me?

+3  A: 
  dataGridView1.Rows.Clear();
        try
        {
            foreach (string fileNames in fileDrive)
            {

                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);

                    new SanitizeFileNames().FileCleanup(fileNames);
                }

                else
                {
                    continue;
                }

            }
        }
STO
new SanitizeFileNames.FileCleanup(fileNames); gives me the error: FileCleanup(string) is a 'method' but used like a 'type'. Do i need to somehow edit my method or class for SanitizeFileNames?
It should be new SanitizeFileNames().FileCleanup(fileNames). (Note the brackets after SanitizeFileNames.)
Anna Lear
ah yes, my mistake. did not notice the brackets. Thank you much!
+1  A: 

The parameter type should be an enumerable collection of some sort: a list or an array would do. Also, strings are immutable so you could return a list of cleaned up filenames:

public class SanitizeFilenames
{
    public List<string> FileCleanUp(IEnumerable<string> filenames)
    {
        var cleanedFileNames = new List<string>();

        var invalidChars = Path.GetInvalidFileNameChars();
        foreach(string file in filenames)
        {
            if(file.IndexOfAny(invalidChars) != -1)
            {
                // clean the file name and add it to the cleanedFileNames list
            }
            else 
            {
                // nothing to clean here
                cleanedFileNames.Add(file);
            }
        }

        return cleanedFileNames;
    }
}
Anna Lear
As you say it should be a collection "of some sort" som please make it just that, actually, it should be a sequence "of some sort" so let's make it an IEnumerable<string> rather than a List<string>.
Patrik Hägne
Fair enough. I updated my answer.
Anna Lear
+1  A: 

I suppose you want to pass a dirty name to the FileCleanup function and get a clean out. Here is how you can do that :

public String FileCleanup(string fileNames)
{
    string regPattern = " *[\\~#%&*{}/<>?|\"-]+ *";
    string replacement = "";
    Regex regExPattern = new Regex(regPattern);

    ...

    return cleanName;
}

and use it in your code like this:

String cleanName = new SanitizeFileNames().FileCleanup(fileNames);

where you put the comment.

decyclone
+1  A: 
  1. You can create a third class static class and add static variable called files “public static List<string> Files= new List<string>()” as example.
  2. When you create the files add the same files to the static variable.
  3. When you clean the files loop throw the static variable, and at the end clear it.
Waleed A.K.