Results 1 to 6 of 6

Thread: [RESOLVED] Directory Searching

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Resolved [RESOLVED] Directory Searching

    I am using Excel 2003 & Access 2003

    I need to search a directory structure looking for any Directories which
    match a specific code. I am looking in all directories under a specified
    directory, I might have as many as 15,000 directories in the tree.

    Thus if I have a directory structure of

    T:\Base Dir
    ---Tree One
    ------Dir 001
    ------Dir 002
    ------Dir 004
    ---Tree Two
    ------Dir 003
    ------Dir 005

    Then searching for a pattern of 001 under T:\ would return
    T:\Base Dir\Tree One\Dir 001

    Whilst searching for it under T:\Base Dir\Tree Two would return an Empty
    value.

    I have tried Application.FileSearch, but it only returns Files, not directories.
    I have also tried Dir, but this comes up with a memory error after about 50 calls.
    I am currently trying SearchScope & ScopeFolder, but cannot find any documentation
    on these, and currently cannot adjust the code from the help files to stop it
    from searching my local computer, when I just want it to search a specified
    directory.

    Can anyone provide a fast searching method for directories only, or a tutorial on SearchScope and ScopeFolder.
    Signature Under Construction

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Directory Searching

    here is a sub using DIR to return all directories that match a search pattern
    i don't have as many folders as you, but had no memory errors when testing

    i modified this from a file search routine

    i returned the results in an array (arrfound) that must be declared globally as the sub is recursive

    Code:
    Sub findsubfolders(spath, spattern As String)
    
    
    Dim myfile As String, dirarr() As String, cnt As Long, i As Long
    
    ReDim dirarr(100)
    cnt = 0
        myfile = Dir(spath & "\*.*", vbDirectory)
            Do While Not myfile = ""
                If Not myfile = "." And Not myfile = ".." Then
                        If GetAttr(spath & "\" & myfile) = vbDirectory Then
    arrfound(UBound(arrfound) + 100)
                        If LCase(myfile) Like LCase(spattern) Then
                            If Not Len(arrfound(0)) = 0 Then ReDim Preserve arrfound(UBound(arrfound) + 1)
                            arrfound(UBound(arrfound)) = spath & "\" & myfile
                        End If
                        If cnt = UBound(dirarr) Then ReDim Preserve dirarr(UBound(dirarr) + 100)
                        dirarr(cnt) = spath & "\" & myfile
                        cnt = cnt + 1
    '                 **** file search
    '                ElseIf LCase(myfile) Like LCase(spattern) Then dostuff spath & "\" & myfile
                    End If
                End If
                myfile = Dir
            Loop
                If Not cnt = 0 Then
                    cnt = cnt - 1
                    ReDim Preserve dirarr(cnt)
                        For i = 0 To UBound(dirarr)
                            findsubfolders dirarr(i), spattern
                        Next
                End If
    End Sub
    Code:
    Sub findfile()
    'set starting folder and files to match
    'reset array
    ReDim arrfound(0)
    Call findsubfolders("C:\Documents and Settings\peter\My Documents", "my*")
    
    'arrfound should contain all the folders (complete path) in the tree below the starting folder, that match the pattern
    End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Directory Searching

    Thanks Westconn,

    I had to make a few adjustments, but the code below works

    However when I ran it against my current live directories, Circa 15,000+, it
    took approx 10 Minutes to run.

    Any ideas as to how it could be speeded up?

    VB Code:
    1. Global arrfound()
    2. Sub findsubfolders(spath, spattern As String)
    3. Dim myfile As String, dirarr() As String, cnt As Long, i As Long
    4.  
    5. ReDim dirarr(100)
    6. cnt = 0
    7.     myfile = Dir(spath & "\*.*", vbDirectory)
    8.         Do While Not myfile = ""
    9.             If Not myfile = "." And Not myfile = ".." Then
    10.                 If (GetAttr(spath & "\" & myfile) And vbDirectory) <> 0 Then
    11.                     If LCase(myfile) Like LCase(spattern) Then
    12.                         If Not IsEmpty(arrfound(UBound(arrfound))) Then
    13.                             ReDim Preserve arrfound(UBound(arrfound) + 1)
    14.                         End If
    15.                         arrfound(UBound(arrfound)) = spath & "\" & myfile
    16.                     End If
    17.                     If cnt = UBound(dirarr) Then ReDim Preserve dirarr(UBound(dirarr) + 100)
    18.                     dirarr(cnt) = spath & "\" & myfile
    19.                     cnt = cnt + 1
    20. '               **** file search
    21. '               ElseIf LCase(myfile) Like LCase(spattern) Then dostuff spath & "\" & myfile
    22.                 End If
    23.             End If
    24.             myfile = Dir
    25.         Loop
    26.             If Not cnt = 0 Then
    27.                 cnt = cnt - 1
    28.                 ReDim Preserve dirarr(cnt)
    29.                     For i = 0 To UBound(dirarr)
    30.                         findsubfolders dirarr(i), spattern
    31.                     Next
    32.             End If
    33. End Sub
    34.  
    35. Sub findfile()
    36. 'set starting folder and files to match
    37. 'reset array
    38. Dim counter As Long
    39. Dim StartTime As Date
    40.     StartTime = Now
    41.     OutputRow = 1
    42.     CountRow = 1
    43. ReDim arrfound(0)
    44.     ThisWorkbook.Worksheets("Sheet1").Cells.Delete
    45.     ThisWorkbook.Worksheets("Sheet2").Cells.Delete
    46. Call findsubfolders("V:\Notes", "*10408*")
    47.  
    48. 'arrfound will now contain all the folders (complete path) in the tree below the starting folder, that match the pattern
    49.     If Not IsEmpty(arrfound) then
    50.         For counter = LBound(arrfound) To UBound(arrfound)
    51.             Debug.Print counter & " :: " & arrfound(counter)
    52.         Next counter
    53.     End if
    54.     Debug.Print StartTime & " :: " & Now
    55. End Sub
    Signature Under Construction

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Directory Searching

    how long does it take to do a similar search in windows explorer?

    the other method that would be simple to code is to use the file system object then you would not need to iterate through all the files, just work with the collections
    i haven't tried to do this, but it would be worth testing, often the search would take as long, but in this case it may be quicker, you would need to add a reference to microsoft scripting runtime

    Code:
    Sub dirs(startpath, pattern)
    Dim fs As New FileSystemObject, sf As Object, dirarr() As String, cnt As Long
    ReDim dirarr(100)
    
    For Each sf In fs.GetFolder(startpath).SubFolders
        dirarr(cnt) = sf.Path
        cnt = cnt + 1
        If LCase(sf.Name) Like LCase(pattern) Then
            If Not Len(foundarr(0)) = 0 Then ReDim Preserve foundarr(UBound(foundarr) + 1)
            foundarr(UBound(foundarr)) = sf.Path
        End If
    Next
    If Not cnt = 0 Then
     ReDim Preserve dirarr(cnt - 1)
        For i = 0 To UBound(dirarr)
            dirs dirarr(i), pattern
        Next
    End If
    End Sub
    Sub finddir()
    ReDim foundarr(0)
    dirs "C:\Documents and Settings\peter\My Documents", "my*"
    
    End Sub
    try some speed test with this
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Directory Searching

    Hi Westconn,

    Windows Explorer took 7 Minutes.
    The first code took approx 10 Minutes.
    The new code took 16 minutes, had to make a few mods, posted below.
    Looks like I will go with the first code you where kind enough to provide.

    Many Thanks,

    Torc

    VB Code:
    1. Sub dirs(startpath, pattern)
    2. Dim fs As New FileSystemObject, sf As Object, dirarr() As String, cnt As Long
    3. Dim check As Long
    4.     ReDim dirarr(100)
    5.     cnt = 0
    6.     For Each sf In fs.GetFolder(startpath).SubFolders
    7.         dirarr(cnt) = sf.Path
    8.         cnt = cnt + 1
    9.         If LCase(sf.Name) Like LCase(pattern) Then
    10.              If Not IsEmpty(arrfound(UBound(arrfound))) Then
    11.                  ReDim Preserve arrfound(UBound(arrfound) + 1)
    12.              End If
    13.             arrfound(UBound(arrfound)) = sf.Path
    14.         End If
    15.         If cnt > UBound(dirarr) Then
    16.             ReDim Preserve dirarr(UBound(dirarr) + 100)
    17.         End If
    18.     Next
    19.     If Not cnt = 0 Then
    20.     ReDim Preserve dirarr(cnt - 1)
    21.         For i = 0 To UBound(dirarr)
    22.             dirs dirarr(i), pattern
    23.         Next
    24.     End If
    25. End Sub
    26. Sub finddir()
    27. Dim StartTime As Date
    28.     StartTime = Now
    29.     ReDim arrfound(0)
    30.  
    31.     dirs "V:\IssuanceDocumentation\Notes", "*10408*"
    32.    
    33.     For counter = LBound(arrfound) To UBound(arrfound)
    34.         Debug.Print counter & " :: " & arrfound(counter)
    35.     Next counter
    36.     Debug.Print StartTime & " :: " & Now
    37. End Sub
    Signature Under Construction

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Directory Searching

    i did some testing myself, 6 loops with dir or fso 1 after the other in the order as listed

    15.03125 fso
    5.984375 dir
    3.21875 dir
    5.421875 fso

    sort of strange results, looks like windows does some sort of caching

    there is al so the option of using API jfindfirstfile and findnextfile, but i doubt if there will be any significant difference
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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