Reputation: 21
I have some code that has to search about 30000 files in several subdirectories to see if any files with a particular extension still exists. It is running across a network to an iSeries share. Any suggestion to make this faster.
Dim _cnt As Int16 = _di.GetFiles("*." + row("extension"), SearchOption.AllDirectories).Length
If _cnt > 0 Then
Dim _msg As String = _dir + " still has " + _cnt.ToString() + " " + row("extension")
MessageBox.Show(_msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
ts_StatusLabel.Text = _msg
Return
End If
Also is there a way to stop the search on the first hit?
Thank you.
Upvotes: 1
Views: 1039
Reputation: 216363
To just know if a file with a particular extension is present in the folder structure you could use DirectoryInfo.EnumerateFiles with Any()
. This starts the enumeration immediately without waiting to load first all files with the specified extension.
Dim extFound = _di.EnumerateFiles("*." & row("extension"), _
SearchOption.AllDirectories).Any()
If extFound Then
Console.WriteLine("File with specific extension found")
End If
Of course this doesn't return the count of files with that extension but it just informs if a file with the specified extension exists.
If you need a count then you could use the Count()
extension. But then you need to wait until the whole search is completed and so it is not very different from your current code.
Dim _cnt = _di.EnumerateFiles("*." + row("extension"), _
SearchOption.AllDirectories).Count()
If _cnt Then
Console.WriteLine("Found " & _cnt & " file/s")
End If
For what it worth, a simple benchmark on my Microsoft Visual Studio
folder (37000 files) searching for files with EXE extension requires 960 milliseconds for Count() and it is not measurable (0 milli) for Any() (On an SSD disk)
Upvotes: 4