Creating a File Search Class

lankymart

New member
Joined
Aug 5, 2004
Messages
3
Location
Somewhere in UK
Programming Experience
3-5
I'm in the process of learning VB.NET and i'm currently trying to get to grips with building a File Search Class. The Class will behave like any object with Properties and methods and events. I have got quite way with the construction of this class but I'm am having problems deciding what would be the best way for it to work and be implemented in my application.

At the moment the class looks something like this (Outline):
VB.NET:
[color=blue]Public Class[/color] FileSearch
	[color=blue]Public Event[/color] StateChanged()
[color=blue]	Public Event[/color] PathChanged()
 
[color=blue]	Public Sub[/color] New()
[color=blue]	Public Sub[/color] New([color=blue]ByVal[/color] Path [color=blue]as[/color][color=blue] String[/color])
[color=blue]	Public Sub [/color][color=black]BeginScan()[/color]
[color=blue]	Private Sub[/color] Start()
[color=blue]	Private Function[/color] PerformScan(SearchPath [color=blue]as String[/color])
[color=blue]	Private Function[/color] GetLogicalDrives() [color=blue]as String[/color]()
 
[color=blue]	Public Property[/color] CurrentPath() [color=blue]as String[/color]
[color=blue]	Public Property[/color] DrivesToScan() [color=blue]as String[/color]()
[color=blue]	Public Property[/color] ScanProgress() [color=blue]as Integer[/color]
[color=blue]	Public Property[/color] ScanState() [color=blue]as[/color] ScanState
[color=blue]End Class[/color]
I am at present adding the following functionality to the class:
  • Recursive file search, with filtering capability.
  • Property reference to the current directory beening searched.
  • The ability to "disable" the search at anytime or exit.
  • Keep track of the files found that match the search criteria.
  • Report when the search has completed or has failed using events.
The class needs to be generic and this is where I am coming unstuck. I can't figure out how best to design and implement the class that will make it re-usable in other applications. Presently I am implementing the class by adding a reference to a new instance of the class in a property of my form called FileSearchData, this allows me to use this reference to call the class's public method BeginScan() which start's the file search routine.

The problems I am currently encounter are as follows:
  • When application is exited if the FileSearch.BeginScan() method has been called the method will not finish until the search is complete.
  • If FileSearch.BeginScan() is called more then once the thread will error. Need to capture the threadstate and work around this.
  • Can't figure out how to implement a progressbar on my form that is updated by the FileSearch object without jeopardising the generic functionality of the class.
If anymore information is needed I will be happy to provide it. Anyone who can help me understand how better to implement this class will be forever in my debt!

Thank you in advance.
 
Last edited:
A little bit more information

Seen as though no one has attempted to help me with this I have decided to add a bit more information to try and coax you developer gurus into giving some ideas about the direction I should go with this.
Below is the code for the private method PerformScan(), this is the guts of the file search operation.

VB.NET:
[/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] PerformScan([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] SearchPath [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Boolean[/color][/size]
[size=2][color=#0000ff]	Dim[/color][/size][size=2] ScanDir [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]String[/color][/size]
[size=2][color=#0000ff]	Dim[/color][/size][size=2] FileName [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]String[/color][/size]
[size=2][color=#0000ff]	Try[/color][/size]
[size=2][color=#0000ff]		For [/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] ScanDir [/size][size=2][color=#0000ff]In[/color][/size][size=2] SearchDir.GetDirectories _[/size]
[size=2]		(SearchPath.Concat(SearchPath, Path.DirectorySeparatorChar))[/size]
[size=2][color=#0000ff]			Me[/color][/size][size=2].CurrentPath = ScanDir[/size]
[size=2]			Application.DoEvents()[/size]
[size=2][color=#0000ff]			If [/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].ScanState = FileSearchState.Disabled [/size][size=2][color=#0000ff]Then[/color][/size]
[size=2][color=#0000ff]				Return [/color][/size][size=2][color=#0000ff]False[/color][/size]
[size=2][color=#0000ff]			End [/color][/size][size=2][color=#0000ff]If[/color][/size]
[size=2][color=#0000ff]			Me[/color][/size][size=2].PerformScan(ScanDir)[/size]
[size=2][color=#0000ff]			For [/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] FileName [/size][size=2][color=#0000ff]In[/color][/size][size=2] SearchDir.GetFiles(ScanDir)[/size]
[size=2][color=#0000ff]				If [/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].ScanState = FileSearchState.Disabled [/size][size=2][color=#0000ff]Then[/color][/size]
[size=2][color=#0000ff]					Return [/color][/size][size=2][color=#0000ff]False[/color][/size]
[size=2][color=#0000ff]				End [/color][/size][size=2][color=#0000ff]If[/color][/size]
[size=2]				Application.DoEvents()[/size]
[size=2][color=#0000ff]			Next[/color][/size]
[size=2][color=#0000ff]		Next[/color][/size]
[size=2][color=#0000ff]	Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception[/size]
[size=2]		ErrorCollection.Add(ex)[/size]
[size=2][color=#0000ff]	End [/color][/size][size=2][color=#0000ff]Try[/color][/size]
[size=2][color=#0000ff]	Return [/color][/size][size=2][color=#0000ff]True[/color][/size]
[size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Function[/color][color=black]
[/color]

Please help me!!
 
Last edited:
I would be happy to help, but I'm not sure what you're trying to accomplish. I'm just a novice, but I'll give it a shot.

I assume you want to scan a computer (the given Drives or path) and keep a catalog of the Files (given some filter) and perhaps some information about the files?

The PerformScan method given above doesn't seem to do anything.

First, I would suggest using a seperate thread to perform the search, that way you can end the thread at any time and your app won't seem to lock up. The Application.DoEvents method is OK in some situations, but I would prefer a seperate thread for this case. See the System.Threading namespace. It sounds like you are doing this, I would need to see specific code to understand what's wrong. You can use the ThreadState and IsAlive properties of the thread to find out information about a specific thread. Also set the IsBackground property of the thread to True if you want it to stop when the calling procedure has ended.

The Property reference to the current directory beening searched should be fairly simple:
VB.NET:
Private _strCurrentDir
Public ReadOnly Property CurrentDirectory() As String
    Get
        Return _strCurrentDir
    End Get
End Property

As far as keeping track of the files found that match the search criteria, use a dataSource (database, XML file, ...).

Report when the search has completed or has failed using events:
VB.NET:
'Define the Event
Public Event SearchFailed(ByVal strReason As String)
Public Event SearchComplete()

'In some procedure
RaiseEvent SearchFailed("path not found")
RaiseEvent SearchComplete()

To implement the progress bar, you can raise an event (with progress percentage and maybe current directory as parameters) when the current directory has been completed (or started). You could also do the same for files.
 
Thank you for taking the time to reply!

Paszt,

Thank you for taking the time to reply to this thread. Since no one was answering I went to another forum and began a thread there in the hope of getting some answers (VBCity) the thread is located at Here, I would really like your input on this issue if you could post your input to this particular thread. The other reason I have done this is the code formatting is much better, I spent a good half hour just trying to get my code to display right.

I understand if you do not want to, but please consider it. If you do post to this thread I will try to respond.

Now back to the point in hand, in answer to your questions:
  • Yes it does appear that PerformScan() method does do nothing, but it is only test code I only posted it as pseudo code to give you an idea of the process. It actually at the moment just updates a label on the form to show the current directory being searched, although from the code it does appear to do even that. However the me.CurrentPath property is what I use to hold the current path being searched, this has attached to it a raise event PathChanged() which calls a function in my main application that refreshes the form display using the object parameters passed to it.
  • Your right I am infact using the a Thread in my BeginScan() method to fire the main guts of the search the PerformScan() method. It's a bit untidy at present the form calls the public method BeginScan() which creates a new thread from a class level reference which calls Start() method, this checks to see what logical drives exist and adds them to a property of the search object called DrivesToScan() (This at present is a string array), then it literates through the string array and calls PerformScan() method against each drive in the DrivesToScan() string array. Not to tidy right??
  • I do not use the IsBackground property of the Thread class though, maybe this is where I am going wrong? I've tryed to capture the threadstate, but even when the search has been performed the thread is still classed as running so I can't seem to do anything with it. This means when the method BeginScan() is called again it produces an error stating that you cannot call a thread that is already running. I admitt am not that familiar with threads so I have never really got the grasp of it, am getting there though, any help is very much appreciated!
  • The database idea for storing the files that match the criteria is something I have been thinking about, however I still need to generic way of storing the value so it can be retrieved by the application which in turn can then add it to a database. That functionality I would to leave to the application and maintain the generic function of the File Search class.

Thank you for all your ideas!
 
Back
Top