What you have is
O(N*M) where
N is the number of names in
arrFileLines
and
M is the total number of files returned by the
Directory.GetFiles
. This can be changed pretty easily to be
O(N+M) ... lots faster.
First, load the
arrFileLines
entries into a HashSet(Of String):
(My VB is worse than rusty...but here goes.)
Dim matchNames As New HashSet(Of String)(arrFileLines)
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
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()
.