File Search



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



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.

Archived Comments

From: Akshit Zaveri
Date: Friday, January 28, 2011 at 16:04:37


Date: Sunday, May 23, 2010 at 04:53:07
Comments: Emm code: If Right(Text1.text,4)=".mp3" then
          End if
From: mephist
Date: Friday, April 30, 2010 at 09:03:24
Comments: Very good job, i'm also a vb6 guy, thank you for your contribution.
From: Tola
Date: Thursday, March 18, 2010 at 04:52:59
Comments: Hi all, I am Cambodian.
Thank so much for your help.
It's look cute to run it.
From: Mik
Date: Saturday, February 6, 2010 at 21:19:13
Comments: Ohhh.. I hope a lot thats its fast.. Im looking foward to add it to my config patcher for a game.. It needs to retrieve the config's directory and copy a file there.. X) thx in advance.. cheers
From: Amirkhon
Date: Friday, August 14, 2009 at 13:17:54
Comments: Hi, Friends
 if you need help please write me i'm happy to help someone
From: David B
Date: Friday, May 22, 2009 at 04:40:31
Comments: El proyecto se podrķa mejorar muchisimo si pusiera a disposicion el codigo fuente del OCX. Lo probe y esta muy interesante. Solo ese detalle, incluso el boton de detener podria buscarle una solucion teniendo el codigo fuente. Felicitaciones igualmente
From: Neo_BeKaBe
Date: Friday, March 20, 2009 at 09:28:56
Comments: Search machine!
From: Nattanan Bunprasert
Date: Tuesday, March 10, 2009 at 22:11:10
Comments: Thank you very much.
From: Gwapo
Date: Thursday, January 29, 2009 at 15:38:26
Comments: wow great!!! thnx
From: Mapunda
Date: Monday, January 26, 2009 at 07:11:21
Comments: Thanx 4 nice job. Keep it up
From: Runescape Forums <>
Date: Thursday, October 9, 2008 at 04:59:18
Comments: hmm looks pretty sweet
From: rassel
Date: Tuesday, August 12, 2008 at 02:05:13
Comments: hey wazzup this is good!!!
From: cihan
Date: Saturday, June 14, 2008 at 16:49:52
Comments: Thanks it is great!
From: question
Date: Monday, February 11, 2008 at 12:56:47
Comments: hey which msdn cd are you talking about?
i have all msdn cd's and i want to add the control to my project so all my users don't have to download the .OCX file.

The project can be found on MSDN Disc 1 under 'SAMPLES\VB98\FILECTS'.

From: ghel
Date: Sunday, January 13, 2008 at 09:59:50
Comments: what do you mean by "Have you put the control on the Form and is it named FS1?"

Jawahar was saying that he got the error message 'Object Required' when attempting to compile the above code.  This code needs a File Search control to be named FS1.  If Jawahar did not have a File Search control called FS1, this may be the cause of the error he was getting.

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 !!!!

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.

Martin Allen 1999 - 2011.  Last updated Wednesday 10 August 2011 07:31:56 PM +0100.