简体   繁体   English

删除目录中文件的最快方法是什么? (特定文件扩展名除外)

[英]What is the fastest way of deleting files in a directory? (Except specific file extension)

I have seen questions like What is the best way to empty a directory?我看到过诸如清空目录的最佳方法是什么之类的问题

But I need to know,但我需要知道,

what is the fastest way of deleting all the files found within the directory, except any .zip files found.删除目录中找到的所有文件的最快方法是什么,找到的任何.zip文件除外。

Smells like linq here... or what?闻起来像这里的 linq ......或者什么?

By saying fastest way, I mean the Fastest execution time.说最快的方式,我的意思是最快的执行时间。

If you are using .NET 4 you can benifit the smart way .NET now parallizing your functions.如果您正在使用 .NET 4,您可以通过 .NET 现在并行化您的功能的智能方式受益。 This code is the fasted way to do it.此代码是执行此操作的快速方法。 This scales with your numbers of cores on the processor too.这也与处理器上的内核数量成比例。

DirectoryInfo di = new DirectoryInfo(yourDir);
var files = di.GetFiles();

files.AsParallel().Where(f => f.Extension != ".zip").ForAll((f) => f.Delete());

By fastest are you asking for the least lines of code or the quickest execution time?您是要求最少的代码行还是最快的执行时间? Here is a sample using LINQ with a parallel for each loop to delete them quickly.这是一个使用 LINQ 的示例,每个循环都有一个并行以快速删除它们。

string[] files = System.IO.Directory.GetFiles("c:\\temp", "*.*", IO.SearchOption.TopDirectoryOnly);

List<string> del = (
   from string s in files
   where ! (s.EndsWith(".zip"))
   select s).ToList();

Parallel.ForEach(del, (string s) => { IO.File.Delete(s); });

At the time of writing this answer none of the previous answers used Directory.EnumerateFiles() which allows you to carry on operations on the list of files while the list is being constructed .在撰写此答案时,以前的答案均未使用 Directory.EnumerateFiles(),它允许您在构建列表时对文件列表进行操作。 Code:代码:

Parallel.ForEach(Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).AsParallel(), Item =>
        {
            if(!string.Equals(Path.GetExtension(Item), ".zip",StringComparison.OrdinalIgnoreCase))
                File.Delete(Item);
        });

as far as I know the performance gain from using AsParallel() shouldn't be significant(if found) in this case however it did make difference in my case.据我所知,在这种情况下,使用 AsParallel() 的性能提升不应该是显着的(如果找到),但是在我的情况下确实有所不同。

I compared the time it takes to delete all but .zip files in a list of 4689 files of which 10 were zip files using 1-foreach.我比较了删除 4689 个文件列表中除 .zip 文件之外的所有文件所需的时间,其中 10 个是使用 1-foreach 的 zip 文件。 2-parallel foreach. 2 并行 foreach。 3-IEnumerable().AsParallel().ForAll. 3-IEnumerable().AsParallel().ForAll。 4-parallel foreach using IEnumerable().AsParallel() as illustrated above.使用 IEnumerable().AsParallel() 的 4 并行 foreach,如上所示。 Results:结果:

1-1545 1-1545

2-1015 2-1015

3-1103 3-1103

4-839 4-839

the fifth and the last case was a normal foreach using Directory.GetFiles()第五个也是最后一个案例是使用 Directory.GetFiles() 的普通 foreach

5-2266 5-2266

of course the results weren't conclusive , as far as I know to carry on a proper benchmarking you need to use a ram drive instead of a HDD .当然,结果并不是决定性的,据我所知,要进行适当的基准测试,您需要使用 ram 驱动器而不是 HDD。

Note:that the performance difference between EnumerateFiles and GetFiles becomes more apparent as the number of files increases.注意:随着文件数量的增加,EnumerateFiles 和 GetFiles 之间的性能差异变得更加明显。

Here's plain old C#这是普通的旧 C#

foreach(string file in Directory.GetFiles(Server.MapPath("~/yourdirectory")))
{
    if(Path.GetExtension(file) != ".zip")
    {
        File.Delete(file);
    }
}

And here's LINQ这是 LINQ

var files = from f in Directory.GetFiles("")
            where Path.GetExtension(f) != ".zip"
            select f;

foreach(string file in files)
    File.Delete(file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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