Solved

A question about Recurse All SubDirectories?

Posted on 2002-06-11
2
129 Views
Last Modified: 2010-05-02
This code will recursively parse a directory defined by an path parameter.

Function RecurseFolderList(foldername)

    Dim fso, f, fc, fj, f1

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(foldername)
    Set fc = f.Subfolders
    Set fj = f.Files

    'For each subfolder in the Folder
    For Each f1 In fc
        'Do something with the Folder Name
        debug.print f1
        'Then recurse this function with the sub
        '-folder to get any sub-folders
        RecurseFolderList(f1)
    Next    
   
    'For each folder check for any files
    For Each f1 In fj
        debug.print f1
    Next

End Function

But I don't know how to stop the recursive function when a SubDirectorie/file is found

Could any can tell me how to do?

Thanks a lot!
0
Comment
Question by:Jerryleo
2 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 7069486
Function RecurseFolderList(foldername, byref pBlnFound as boolean)

   Dim fso, f, fc, fj, f1

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(foldername)
   Set fc = f.Subfolders
   Set fj = f.Files

   'For each subfolder in the Folder
   For Each f1 In fc
       'Do something with the Folder Name
       debug.print f1
       if f1 = "what you are looking for" then
           pblnfound = true
       else
           'Then recurse this function with the sub
           '-folder to get any sub-folders
           RecurseFolderList(f1, pblnfound)
       endif
       if pblnfound then exit for
   Next    
   
   'For each folder check for any files
   For Each f1 In fj
       debug.print f1
   Next

End Function
0
 

Author Comment

by:Jerryleo
ID: 7075692
Thank you very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now