本文介绍了如何从另一个目录中的2,50,000个文件中使用文件名收集3000个特定文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过这段代码,但执行需要2-3个小时才能让它快速完成?



I have try this code but it take 2-3 hours to execute can we make it fast ??

For Each _FileName As String In arrFileLines
     For Each _file As String In Directory.GetFiles(sSourcePath, "*.*", SearchOption.AllDirectories)
        If _FileName = Path.GetFileName(_file) Then
                  File.Copy(_file, Path.Combine(sDestPath, _FileName))
        End If
     Next
 Next





其中arrFileLine存储搜索文件的名称,sSourcePath是serching目录,sDestPath是目标direcotry。



Where arrFileLine store the name of search files and sSourcePath is serching directory and sDestPath is destination direcotry.

推荐答案


Dim matchNames As New HashSet(Of String)(arrFileLines)



这是 O(N),它仅取决于 arrFileLines 的长度,并且只完成一次。 (实际上,它可以在任何循环中初始化,将值转换为 arrFileLines 而不是构建该数组(除非数组有其他用途)。)



然后使用 Directory.EnumerateFiles 一次扫描所有文件并检查匹配项。 (使用 Directory.EnumerateFiles 而不是 Directory.GetFiles 因为它不需要返回巨大的所有文件数组。)


This is O(N), it depends only on the length of arrFileLines, and is done only once. (In fact, it could be initialized in whatever loop gets the values into arrFileLines instead of building that array (unless the array has additional uses).)

Then scan through all of the files with Directory.EnumerateFiles once and check for matches. (Use Directory.EnumerateFiles instead of Directory.GetFiles because it doesn't require returning all of the files in a huge array.)

For Each _file As String In Directory.EnumerateFiles(sSourcePath, "*.*", SearchOption.AllDirectories)
  Dim fileName as String = Path.GetFileName(_file)
  If matchNames.Contains(fileName) Then
    File.Copy(_file, Path.Combine(sDestPath, fileName))
  End If
Next



HashSet 允许在固定时间内立即检查 arrFileLines 中的所有名称。

这部分是 O(M),因为它仅取决于 Directory.EnumerateFiles()返回的文件总数。


The HashSet allows for checking all of the names in arrFileLines at once in constant time.
This part is O(M), as it depends only on the total number of files returned by the Directory.EnumerateFiles().



这篇关于如何从另一个目录中的2,50,000个文件中使用文件名收集3000个特定文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:16