searching recursively through directories
From: Joe (Joe_at_discussions.microsoft.com)
Date: 09/28/04
- Next message: mark_at_bdk: "VB4 on Windows 2000"
- Previous message: Brian H.: "RE: Need list of form events that always happen?"
- Next in thread: Ken Halter: "Re: searching recursively through directories"
- Reply: Ken Halter: "Re: searching recursively through directories"
- Reply: Larry Serflaten: "Re: searching recursively through directories"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: mark_at_bdk: "VB4 on Windows 2000"
- Previous message: Brian H.: "RE: Need list of form events that always happen?"
- Next in thread: Ken Halter: "Re: searching recursively through directories"
- Reply: Ken Halter: "Re: searching recursively through directories"
- Reply: Larry Serflaten: "Re: searching recursively through directories"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|