searching recursively through directories

From: Joe (Joe_at_discussions.microsoft.com)
Date: 09/28/04


Date: Tue, 28 Sep 2004 07:51:03 -0700

I am writing some code to locate files (exe's, dll's, etc) in a given root
directory and its subdirectories. For the most part I've been successful.
The problem that I am having is on the line tFile = Dir$ in the GetFiles
subroutine below.

To test the code, I created a root directory ("C:\IFSDocs") with a
subdirectory ("C:\IFSDocs\ABC"). There is an ini file in the root
("C:\IFSDocs") and an exe and a dat file in the subdirectory
("C:\IFSDocs\ABC"). The code locates the ini w/o any difficulty and locates
the dat and exe files in ABC w/o any difficulty. The problem is that when I
am leaving the first recursive call to GetFiles. I think that this is
becuase of the previous call that I made to the Dir function. I'm not sure
if there is an elegant way to handle this. If you know of one please let me
know.

Here's the code:

Public Sub Main()
    Dim colFiles As Collection
    Set colFiles = New Collection
    Call GetFiles(colFiles, "C:\IFSDocs", "*.exe;*.ini;*.dat", True)
    Set colFiles = Nothing
End Sub

Public Sub GetFiles(ByRef poLocatedFiles As Collection, _
                        ByVal ptPath As String, _
                        ByVal ptFileTypes As String, _
                        Optional pfRecurseSubDirectories As Boolean)
    
    Dim taFileMask() As String
    Dim tFile As String
    Dim i As Integer
    
    If Right$(ptPath, 1) <> "\" Then ptPath = ptPath & "\"
    taFileMask() = Split(ptFileTypes, ";")
    
    ' check for files in the specified directory
    For i = LBound(taFileMask) To UBound(taFileMask)
        tFile = Dir$(ptPath & taFileMask(i))
        Do While Len(tFile) > 0
            tFile = ptPath & tFile
            poLocatedFiles.Add tFile, tFile
            tFile = Dir$
        Loop
    Next i
    
    If pfRecurseSubDirectories Then
        tFile = Dir$(ptPath & "*.*", vbDirectory) '
get the subdirectory
        Do While Len(tFile) > 0 '
found subdirectory
            If tFile <> "." And tFile <> ".." Then '
exclude "." and ".."
                If (GetAttr(ptPath & tFile) And vbDirectory) <> 0 Then '
ignore regular files
                    tFile = ptPath & tFile '
include this directory
                    Call GetFiles(poLocatedFiles, tFile, ptFileTypes, True)
                End If
            End If
            tFile = Dir$ '
get next directory
        Loop
    End If
End Sub

TIA, and thanks to Veign for providing the help to get this far.

-- 
Joe
VBA Automation/VB/C++/Web and DB development


Relevant Pages

  • Re: How to get the page name of itself
    ... That will return the page name *if* it's in the root. ... Public Sub Page_Load ... Dim strURL, arrayURL, pagename ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: getting an absolute path
    ... Public Sub Page_Load ... Label1.Text = root ... Juan T. Llibre, ASP.NET MVP ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: VB.NET - Set path for DLLs
    ... Application Configuration File". ... in the root of your project. ... bin directory called YourProject.exe.config (that matches your ... >> Public Sub Main ...
    (microsoft.public.dotnet.languages.vb)