简体   繁体   中英

Delete file from folder where contains

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?

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.

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

我不确定您是否要从文件夹中删除,但是正如您所提到的,您想从fileEntries集合中删除可以通过此方法实现

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

您可能希望查看System.IO-Class来删除文件( http://msdn.microsoft.com/zh-cn/library/System.IO(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM