How would i search for a file by it's extension i.e .doc and have all the results be displayed in a list box?
Carl
Printable View
How would i search for a file by it's extension i.e .doc and have all the results be displayed in a list box?
Carl
search 1 folder? entire drive?
See Post #7 in this thread which is just a couple of threads below this one. :DQuote:
Originally Posted by VB4Eva
the below code will search entire drive for whatever you type into text1...
Listing the results in a listbox... (Allows wildcards ;) )
so search for *.jpg will find any JPG files
VB Code:
Dim FileName As String Public Function RecurseFolderList(FolderName As String) As Boolean On Error Resume Next Dim fso, f, fc, fj, f1 Set fso = CreateObject("Scripting.FileSystemObject") If Err.Number > 0 Then RecurseFolderList = False Exit Function End If On Error GoTo 0 If fso.FolderExists(FolderName) Then Set f = fso.GetFolder(FolderName) Set fc = f.Subfolders Set fj = f.Files For Each f1 In fc RecurseFolderList (f1) Next For Each f1 In fj Me.Caption = f1 If f1 Like FileName Then List1.AddItem f1 End If DoEvents: DoEvents Next Set f = Nothing Set fc = Nothing Set fj = Nothing Set f1 = Nothing Else RecurseFolderList = False End If Set fso = Nothing End Function Private Sub Command1_Click() FileName = Text1 RecurseFolderList "C:\" End Sub
Thanks for all of your help but i get this error when i try it:
VB Code:
For Each f1 In fc
Carl
What error do you get?Quote:
Originally Posted by VB4Eva
Run Time Error "70"
Permission Denied.
do u have any security on any of the folders?
Are these network folders?Quote:
Originally Posted by VB4Eva
I'm using the code on my college network. But it should not stop it from working should it?
it will cause problems when it hits a folder that u do not have permission to view..
On error goto errHandler...
and a bit lower....
errHandler:
if Err.number = 70 then
'Log the event ?
Resume next
end if
Hi there guys, it works now although it does not search for *.doc files. When i try it, it lists every single file on my hard drive.
Is there anyway i can fix this. Also, how would i get the number of items in a list box to display in a label? Also when it has finished searching all the files on my drive how will i stop it from looping?
Sorry about all the questions i'm just curious. You don't have to help if you do not want too :)
Carl
This is how I did it:
VB Code:
For Each f2 In fj If LCase(Right(f2.Name, 3)) = "doc" Then
each time u "Add" to te listbox
Label1.Caption = List1.Listcount
it should finish all on its on when it completes the search.
no need to worry about that
help pls
i tried the code above and its working my question is how can i view the directory of the file where i found the file
ex.
"d:\test\test.bmp" --> this is what apper in the list box
"d:\test\" --- > this is what i want to appear in the listbox or i want to appear that in another control like label box
thanks!!!!!
sorry for the poor english
VB Code:
Private Declare Function ShellEx Lib "shell32.dll" Alias _ "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As Any, _ ByVal lpDirectory As Any, ByVal nShowCmd As Long) As Long Private Sub List1_DblClick() Dim tmp As String Dim pth As String tmp = List1.List(List1.ListIndex) pth = Left(tmp, InStrRev(tmp, "\")) ShellEx Form1.hwnd, "open", pth, "", "", 1 End Sub