Well, I'm writing a piece of code that will take a list of file names and should search a local drive for each file, determine if it's there or not, and return an array of all the paths found.
Right now I'm using a function that searches for one file and I call this function for each file in the list:
Public Function DriveSearch(ByVal sDir As String, sFile As String) As String()
Dim filesFound As New List(Of String)
Try
For Each dir As String In Directory.GetDirectories(sDir)
For Each file In Directory.GetFiles(dir, sFile)
filesFound.Add(file)
Next
filesFound.AddRange(DriveSearch(dir, sFile))
Next
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return filesFound.ToArray
End Function
But I'm wondering if there's a faster way to achieve this. Maybe I should index all the files in the drive first before searching? (I'm not sure how to do this yet).
The number of files in the list is around 100 (for now) but it's likely to increase to couple thousands.
Edit:
I decided that I won't need the duplicates, so here's my current function:
Public Function DriveSearch(ByVal sDir As String, sFile As String) As String
Dim filePath As String = String.Empty
Try
Dim arrFiles As String() = Directory.GetFiles(sDir, sFile)
If arrFiles.Length > 0 Then
filePath = arrFiles(0)
Else
For Each dir As String In Directory.GetDirectories(sDir)
filePath = DriveSearch(dir, sFile)
If filePath.Length > 0 Then Exit For
Next
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return filePath
End Function
Still not sure if this is the fastest way to search for 100+ files.