martin2k

Google
 
Web www.martin2k.co.uk

File Search


 

Home

Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here

Contact


 

ActiveX Control

This is an ActiveX Control that I compiled using some code that I found on an MSDN CD-ROM.  The control allows you to search for files in a whole drive or starting at a subdirectory.  You can download the latest version here or view the readme file here.

This control has no properties or events, it just has one method - SearchFile.

Create a Form that looks like this:

Create three Label controls, two TextBoxes, a button and a ListBox and arrange them as shown above.

Private Sub Command1_Click()
If Text1.Text = "" Then Exit Sub 'If the textbox is empty, then nothing can be searched for
List1.Clear 'Clear any previous searches
Me.Enabled = False
FS1.SearchFile List1, Text2.Text, Text1.Text, Me
Me.Enabled = True
Me.Caption = "Form1" 'Clear the status bar after the search
End Sub

When the user clicks on the search button after a file pattern has been entered, the control will start searching for files.  The Form's (Me) caption property will be set to the directory that is currently being searched - e.g. "Searching C:\WINDOWS".  When the search is done, the results will be shown in the ListBox.  Anything that has a Caption property can be used here e.g. Label1.

The syntax for the SearchFile method is Object.SearchFile <Listbox>, <Search path>, <File pattern>, <Label>

The ListBox can be also be a ComboBox or any control that supports the .AddItem method.  The search path can be a whole drive - "C:\", or a folder - "D:\Ents".  The file pattern can be all files - "*.*", files with a particular extension - "*.doc", a specific file - "letter.doc" or any other string supported by the FileListBox e.g. "*.wav;*.mid".

Because of the way in which the control was written, you cannot interrupt the search i.e. closing the program in the middle of a search.  Therefore, you should disable the form and then enable it with the search code in the middle.  Interrupting the search will cause strange message boxes to appear from the control itself.


Comments

From: mahmed <lordoftherings92001>
Date: Friday, December 7, 2007 at 13:33:13
Comments: i want to thx u for that control
 
From: thye
Date: Sunday, August 19, 2007 at 10:42:41
Comments: What is 'Me' actually..
 
Reply: 'Me' refers to the Form you are writing the code in.  For the above code, it is the equivalent of 'Form1'.
 
From: Sameera
Date: Sunday, June 10, 2007 at 08:50:23
Comments: Hi... This works excellent... But In my form I want only the names of the file... As I am using combo box to help the user choose a particular file..

For example: "d:\spkfiles" has all "*.spk" files and at the back end I want to assign the file name to a variable for further use.

As in if for example after searching, in the combo box it has found "d:\spkfiles\sam.spk" file. I need only "sam" listed or even if it is not listed I should be somehow able to extract only the name "sam" and assign it to a variable at the back end...

Can Anyone help me with my problem ??? I hope my problem is clear. i want to use the name of the file... and not its entire path !!!!
 
Reply:

To do this, you need to specify a hidden ListBox for the results of the search.  You then need to create a loop to add only the last part of the filename to a visible ListBox or ComboBox.  You can do this with the following code:

For i = 0 To lstHid.ListItems - 1 'hidden ListBox
    l = lstHid.List(i)
    Combo1.AddItem = Right(l, Len(l) - InStrRev(l, "\"))
Next i

The ListIndex property of the selected item in the visible list is used as the reference to get the full filename from the hidden list.

 
From: Ramesh
Date: Thursday, March 29, 2007 at 12:15:36
Comments: This works ok, but search is not similar to windows search. If there are 4 files abc2 03.txt, abc2 04.txt, abc20 03.txt,abc20 04.txt, if search for files in windows with filter as abc20*.txt, it'll return last 2 files, but ur control returns all 4 files.....
 
From: simon
Date: Wednesday, December 13, 2006 at 08:46:15
Comments: exelent work. keep it up!!!!!
 
From: Jawahar
Date: Monday, June 26, 2006 at 09:08:37
Comments: hI WHEN I COMPILE ABOVE code the following Error Messge appeared:
Error Message:Object Required
Error in Line:FS1.SearchFile
 
Reply: Have you put the control on the Form and is it named FS1?
 
From: ryan
Date: Sunday, August 28, 2005 at 05:22:31
Comments: How could I search hidden folder as well?
 
Reply: This control can only search a hidden folder if one is specified as the Search path.  It cannot find folders nested in the Search path e.g. if "C:\WINDOWS" is the Search path, the "INF" folder would be missed even though it contains files, which are not hidden.
 
From: BT/Sandwich
Date: Sunday, August 14, 2005 at 18:14:29
Comments: What sort of "object" are you talking about?
 
Reply: The Object refers to the File Search object.  The download link is at the top of this page.
 
From: Ed
Date: Thursday, May 19, 2005 at 19:55:38
Comments: Hey! this is great! found over 2000 files in under 5 seconds. excelent
 
From: Allen
Date: Tuesday, January 25, 2005 at 20:45:26
Comments: What is I wanted to add a button to stop a serach in progress, does this control support that? Such as FS1.Stop
 
Reply: Unfortunately I did not include this when I compiled this control.  As the code is available on the MSDN CD-ROM, you could look for it on there and use it in your program.  That way, you could change it to suit you.

Please fill in the below form if you have any comments or additional information you want to add about the above information.  If you have any Visual Basic 6.0 questions, please visit the ForumAny questions sent using this form will be ignored.

* - Mandatory

*Name: E-mail:

*Comments:

*Please type the following code (your comment will not be accepted without it):


Martin Allen 1999 - 2008.  Last updated Sunday 14 September 2008 02:51:36 PM +0100.