Question

I am looking to create a Method which I can use to delete files from a directory when the filename contains the wildcard parameter.

So far I have the below but I cannot seem to work out how I can delete any files from my fileEntries collection that contain the wildcard.

public static void DeleteFileContaining(string targetDirectory, string wildcard)
        {
            // Process the list of ALL files found in the directory. 
            string[] fileEntries = Directory.GetFiles(targetDirectory);

            foreach (var item in fileEntries)
            {               
                var itemToDelete = fileEntries.Contains(wildcard);
                 // delete where contains                
            }
        }

Can anyone finish this or suggest a more efficient way of doing this in one neat method call which takes the directory and wildcard?

Was it helpful?

Solution

You can use the search-pattern of GetFiles/EnumerateFiles(which can be more efficient):

public static void DeleteFileContaining(string targetDirectory, string wildcard)
{
    string searchPattern = string.Format("*{0}*", wildcard);
    var filesToDelete = Directory.EnumerateFiles(targetDirectory, searchPattern);
    foreach (var fileToDelete in filesToDelete)
    {
        try{
            File.Delete(fileToDelete);
        }catch(Exception ex){
            // log this...
        }
    }
}

Look at the remarks section for further information.

OTHER TIPS

First off, this line is wrong:

var itemToDelete = fileEntries.Contains(wildcard);

This returns a boolean indicating whether at least one of the filenames is an exact match. What you want is:

var items = fileEntries.Where(name => name.Contains(wildcard));

Second, you don't even need to filter the filenames like this, you can simply use this other GetFiles overload that takes a search pattern.

Finally, you can then use File.Delete to actually delete those files.

This is my first stab at something like this with LINQ, but this worked for me:

public static void DeleteFileContaining(string targetDirectory, string wildcard)
{
    Directory.GetFiles(targetDirectory).Where(j => j.Contains(wildcard)).ToList().ForEach(i => File.Delete(i));
}

Or this uses a multiline lambda expression to include error handling:

public static void DeleteFileContaining(string targetDirectory, string wildcard)
{
    Directory.GetFiles(targetDirectory).Where(j => j.Contains(wildcard)).ToList().ForEach(i =>
        {
            try
            {
                File.Delete(i);
            }
            catch (Exception ex)
            {                        
                //do something here on exception.
            }
        });
}

I would use the wildcard to get only the files that I am interested in.

public static void DeleteFileContaining(string targetDirectory, string wildcard){
        string[] fileEntries = Directory.GetFiles(targetDirectory, wildcard);//something like *.jpg

        // delete all matching files. 
        foreach (string f in fileEntries)
        {
            File.Delete(f);
        }
}

I am not sure you want to delete from folder but as you have mentioned you want to remove from fileEntries collection you can achieve by this

fileEntries = fileEntries.Where(a => !a.Contains(wildcard)).ToArray();

You might want to look at the System.IO-Class to delete files (http://msdn.microsoft.com/en-us/library/System.IO(v=vs.110).aspx)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top