Home » WPFRSS

Search for file anywhere on disk

I see and try many example I find in search threads .   But all very slow !   I try to find file that can be anywhere on harddrive using recursion .   User enter filename and start directory and click button for search.   I have luck to do this but if give start directory of  C:\   examples I try sometime take many minutes to complete .   This is not good for me !   I hope someone can show me example or web site for do this very fast .   Thank you good people !
 

8 Answers Found

 

Answer 1

This sounds like just a limitation of the speed a computer has to traverse the entire file  system and look for a file. There is no getting around the time it takes to look at every single file on the computer to see if it matches what you are looking for.

You could tap into the windows search  API which is present on Vista and Windows 7 (and XP but was not baked into the OS, but a separate download).

With Windows Search, you get the benefit of indexed data so results come back faster, however it is never the default that the ENTIRE drive is indexed, so this would still likely take a little while when it has to search over non indexed folders.

You could possibly try to speed things up by maybe getting all the root folders on the C drive, and firing off separate threads  to start  recursing each root folder, however hard drives just tend to be slow  still (usually the slowest part in the PC)

 

Answer 2

Hello Matt ,   I hoping the reason for search  so slow  because of way I try to do [and maybe my bad programmming skill].   However I feel not so bad since I read what you say and know it not really my fault .

I think about use API function for help make speed up but do not know which to use .   Cannot do only API of Vista or WIN7 because want my app run on XP too .

I also think about use BackGroundWorker find file  while user  do another things .   Have 3 file that must be found on a system and they can be located in any folder anywhere on harddisk .   Maybe I do go with BackgroundWorker if cannot find  Win API correct search function or another faster code method from that I have now .   Thank you very much !
 

Answer 3

Dear Omar,

Please look at this example, it demonstrates how to use the FileSystem.FindInFiles method and return a collection of strings representing the names of files containing the specified text. It's nice and quick on my machine.

Cheers,

John

    Public Sub FindFile(ByVal MyFileText As String, ByVal MyRootFolder As String)
        Dim file_names As ReadOnlyCollection(Of String)
        Dim directory  As String = MyRootFolder 'c:\ for instance
        Dim searchFor As String = MyFileText 'Look for files that contain MyFileText

        file_names = My.Computer.FileSystem.FindInFiles(directory, searchFor, True, FileIO.SearchOption.SearchAllSubDirectories)
        For Each file_name As String In file_names
            Console.WriteLine(file_name)
        Next file_name
    End Sub
 

Answer 4

Hello John,  Thank you for respond !   I try your code with modify to show  files in listbox ,  but not work .   Here what I use in test project .

Imports System.Collections.ObjectModel

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FindFile("mspaint", "C:\")
    End Sub

    Public Sub FindFile(ByVal MyFileText As String, ByVal MyRootFolder As String)
        Dim file_names As ReadOnlyCollection(Of String)
        Dim directory  As String = MyRootFolder 'c:\ for instance
        Dim searchFor As String = MyFileText 'Look for files that contain MyFileText
        Try
            file_names = My.Computer.FileSystem.FindInFiles(directory, searchFor, True, FileIO.SearchOption.SearchAllSubDirectories)
            For Each file_name As String In file_names
                ListBox1.Items.Add(file_name)
            Next
        Catch ex As UnauthorizedAccessException
        End Try
    End Sub
End Class


When click  button,  nothing happen .   I try to find  a file ,  and not text inside a file  .   Maybe that why this code not work ?


 

Answer 5

Try this.  Start a new WF application and add a button  and a listbox to the Form1.  Replace the code on Form1 with:

Imports System.ComponentModel
Imports System.IO
Public Class Form1
  
Dim FileFinders As New List(Of FileFinder)
  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.Enabled = 
False
    FindFiles("C:\""*.gif")
  
End Sub
  Sub FindFiles(ByVal StartDir As StringByVal FileName As String)
    
For Each D As String In Directory.GetDirectories(StartDir)
      
Try
        Dim FileFinder As New FileFinder()
        
AddHandler FileFinder.RunWorkerCompleted, AddressOf FileFinder_Completed
        FileFinders.Add(FileFinder)
        FileFinder.RunWorkerAsync(
New String() {D, FileName})
      
Catch
      End Try
    Next D
  
End Sub
  Sub FileFinder_Completed(ByVal sender As ObjectByVal e As RunWorkerCompletedEventArgs)
    ListBox1.Items.AddRange(
CType(e.Result, List(Of String)).ToArray)
    FileFinders.Remove(
CType(sender, FileFinder))
    
If FileFinders.Count = 0 Then Button1.Enabled = True
  End Sub
End
 Class
Public
 Class FileFinder
  
Inherits BackgroundWorker
  
Dim L As New List(Of String)
  
Protected Overrides Sub OnDoWork(ByVal e As System.ComponentModel.DoWorkEventArgs)
    
MyBase.OnDoWork(e)
    
Try
      GetDirectoryFiles(CType(e.Argument, String()))
    
Catch
    End Try
    e.Result = L
  
End Sub
  Sub GetDirectoryFiles(ByVal SearchInfo() As String)
    L.AddRange(Directory.GetFiles(SearchInfo(0), SearchInfo(1), SearchOption.TopDirectoryOnly))
    
For Each D As String In Directory.GetDirectories(SearchInfo(0))
      
Try
        GetDirectoryFiles(New String() {D, SearchInfo(1)})
      
Catch
      End Try
    Next D
  
End Sub
End
 Class

 

Answer 6

Hello JohnWein ,    Thank you for your reply !   I try your code and it give  exact result I want and fast  more than other code I try .   Now I try modify your code into 1 Class for add to my project so I can call 3 time for search  3 files my app require .   Thank you sir .

O
m
a
r
 

Answer 7

Here's a version that takes an array for the search  pattern:

Imports System.ComponentModel
Imports System.IO
Public Class Form1
  
Dim FileFinders As New List(Of FileFinder)
  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.Enabled = 
False
    FindFiles("C:\"New String() {"*.gif""*.jpg""*.png"})
  
End Sub
  Sub FindFiles(ByVal StartDir As StringByVal FileNames() As String)
    
For Each D As String In Directory.GetDirectories(StartDir)
      
Try
        Dim FileFinder As New FileFinder()
        
AddHandler FileFinder.RunWorkerCompleted, AddressOf FileFinder_Completed
        FileFinders.Add(FileFinder)
        FileFinder.RunWorkerAsync(
New KeyValuePair(Of StringString())(D, FileNames))
      
Catch
      End Try
    Next D
  
End Sub
  Sub FileFinder_Completed(ByVal sender As ObjectByVal e As RunWorkerCompletedEventArgs)
    ListBox1.Items.AddRange(
CType(e.Result, List(Of String)).ToArray)
    FileFinders.Remove(
CType(sender, FileFinder))
    
If FileFinders.Count = 0 Then Button1.Enabled = True
  End Sub
End
 Class
Public
 Class FileFinder
  
Inherits BackgroundWorker
  
Dim L As New List(Of String)
  
Protected Overrides Sub OnDoWork(ByVal e As System.ComponentModel.DoWorkEventArgs)
    
MyBase.OnDoWork(e)
    
Try
      GetDirectoryFiles(CType(e.Argument, KeyValuePair(Of StringString())))
    
Catch
    End Try
    e.Result = L
  
End Sub
  Sub GetDirectoryFiles(ByVal KV As KeyValuePair(Of StringString()))
    L.AddRange(
My.Computer.FileSystem.GetFiles(KV.Key, FileIO.SearchOption.SearchTopLevelOnly, KV.Value))
    
For Each D As String In Directory.GetDirectories(KV.Key)
      
Try
        GetDirectoryFiles(New KeyValuePair(Of StringString())(D, KV.Value))
      
Catch
      End Try
    Next D
  
End Sub
End
 Class



 

Answer 8

John,

This is interesring....however i dont like the filesystemwatcher. Windows as a whole slowly moves away from being a real-time Operating system.

Renee

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter