Results 1 to 17 of 17

Thread: File Search

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    File Search

    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

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: File Search

    search 1 folder? entire drive?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: File Search

    Quote Originally Posted by VB4Eva
    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
    See Post #7 in this thread which is just a couple of threads below this one.

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: File Search

    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:
    1. Dim FileName As String
    2. Public Function RecurseFolderList(FolderName As String) As Boolean
    3.    
    4.     On Error Resume Next
    5.     Dim fso, f, fc, fj, f1
    6.    
    7.     Set fso = CreateObject("Scripting.FileSystemObject")
    8.    
    9.     If Err.Number > 0 Then
    10.         RecurseFolderList = False
    11.         Exit Function
    12.     End If
    13.     On Error GoTo 0
    14.     If fso.FolderExists(FolderName) Then
    15.         Set f = fso.GetFolder(FolderName)
    16.         Set fc = f.Subfolders
    17.         Set fj = f.Files
    18.         For Each f1 In fc
    19.             RecurseFolderList (f1)
    20.         Next
    21.         For Each f1 In fj
    22.             Me.Caption = f1
    23.             If f1 Like FileName Then
    24.                 List1.AddItem f1
    25.                
    26.             End If
    27.             DoEvents: DoEvents
    28.         Next
    29.         Set f = Nothing
    30.         Set fc = Nothing
    31.         Set fj = Nothing
    32.         Set f1 = Nothing
    33.        
    34.     Else
    35.         RecurseFolderList = False
    36.     End If
    37.     Set fso = Nothing
    38. End Function
    39.  
    40.  
    41.  
    42. Private Sub Command1_Click()
    43. FileName = Text1
    44. RecurseFolderList "C:\"
    45.  
    46. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Re: File Search

    Thanks for all of your help but i get this error when i try it:

    VB Code:
    1. For Each f1 In fc

    Carl

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: File Search

    Quote Originally Posted by VB4Eva
    Thanks for all of your help but i get this error when i try it:

    VB Code:
    1. For Each f1 In fc

    Carl
    What error do you get?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Re: File Search

    Run Time Error "70"

    Permission Denied.

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: File Search

    do u have any security on any of the folders?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: File Search

    Quote Originally Posted by VB4Eva
    Run Time Error "70"

    Permission Denied.
    Are these network folders?

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Re: File Search

    I'm using the code on my college network. But it should not stop it from working should it?

  11. #11
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: File Search

    it will cause problems when it hits a folder that u do not have permission to view..
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  12. #12
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: File Search

    On error goto errHandler...

    and a bit lower....

    errHandler:
    if Err.number = 70 then
    'Log the event ?
    Resume next
    end if

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    69

    Re: File Search

    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
    Last edited by VB4Eva; Nov 22nd, 2005 at 04:26 PM.

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: File Search

    This is how I did it:

    VB Code:
    1. For Each f2 In fj
    2.               If LCase(Right(f2.Name, 3)) = "doc" Then

  15. #15
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: File Search

    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
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  16. #16
    Lively Member
    Join Date
    Feb 2006
    Posts
    95

    Re: File Search

    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
    Last edited by makmaks; Feb 27th, 2006 at 11:23 AM.

  17. #17
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: File Search

    VB Code:
    1. Private Declare Function ShellEx Lib "shell32.dll" Alias _
    2. "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    3. ByVal lpFile As String, ByVal lpParameters As Any, _
    4. ByVal lpDirectory As Any, ByVal nShowCmd As Long) As Long
    5.  
    6.  
    7. Private Sub List1_DblClick()
    8.     Dim tmp As String
    9.     Dim pth As String
    10.     tmp = List1.List(List1.ListIndex)
    11.     pth = Left(tmp, InStrRev(tmp, "\"))
    12.     ShellEx Form1.hwnd, "open", pth, "", "", 1
    13. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width