FileSystemObject = User-defined type?

S

SusanV

Hi all,

Access 2000, I'm trying to use a code sample to parse the contents of a
directory, but am getting a compile error on

Dim fso As FileSystemObject

Do I need to set a specific reference to use a FileSystemObject??? I've
wasted a good hour searching the web and none of the snippets or samples
mention setting any kind of reference.

TIA,

SusanV
 
D

Douglas J. Steele

Are you sure you need to use FSO? I've only ever found one case where FSO
could do something that I couldn't do using VBA and/or API calls (the
ability to get the LastUpdated date for a folder)
 
S

SusanV

Good Morning Doug,

What I'm trying to do is populate a table based on the filenames found in a
directory tree, then (hopefully) create a search form so users can find
these files. Files are added (and sometimes moved) by other folks, and we
don't have control over the directory - and it's driving my guys nuts trying
to locate stuff.

Perhaps there is an easier way - but using VBA's Dir function doesn't seem
to return subfolder contents, as does fso - is there some switch or syntax
I'm missing?

TIA,

Susan
 
S

SusanV

Hey Phil!

Long time no see - hope all is well with you and yours! Thanks for the
links - I'm sure they'll be helpful!

;-)

SusanV

Phillip Windell said:
Hoowwdy stranger!

I did these two scripts a couple years ago. They were used in ASP to scan
through files in a folder on a website then look inside the files for
specific things ("Include file" references). But the main thing is that
they do the file/folder recursion and that part of the code should be the
same.

http://darkfalz.com/scripts_details.aspx?pstid=1081
http://darkfalz.com/scripts_details.aspx?pstid=1084


--
Phillip Windell [MCP, MVP, CCNA]
www.wandtv.com



SusanV said:
Nevermind, was missing the Scripting runtime reference.

Doh!

;-D
 
D

Douglas J Steele

It's not intuitive, but yes, Dir can do what you're looking to do.

You need to code a recursive routine. Since you can't nest Dir calls, you
need to store the subfolders in a collection (or an array), and then process
each subfolder once you're done with the files of the parent.


Public Sub ListFilesUsingDir( _
StartDir As String, _
FileList As Collection _
)

Dim strFile As String
Dim strFolder As String
Dim strSubfolder As String
Dim varFolder As Variant
Dim colSubfolders As Collection

' Ensure the starting folder is
' correctly formatted
strFolder = QualifyFolderPath(StartDir)
If Len(strFolder) > 0 Then

' Add each of the files in the folder
' to the Collection
strFile = Dir$(strFolder & "*.*")
Do While Len(strFile) > 0
FileList.Add strFolder & strFile
strFile = Dir$
Loop

' Build a list of subfolders in the folder,
' adding each one to a local collection
Set colSubfolders = New Collection
strSubfolder = Dir$(strFolder, vbDirectory)
Do While Len(strSubfolder) > 0
If (strSubfolder <> ".") And _
(strSubfolder <> "..") Then
If (GetAttr(strFolder & strSubfolder) _
And vbDirectory) = vbDirectory Then
strSubfolder = strFolder & strSubfolder
colSubfolders.Add strSubfolder
End If
End If
strSubfolder = Dir$
Loop

' Process each subfolder found above recursively
' Note that you must use a variant when
' enumerating the elements of a collection,
' but our function is expecting a string.
For Each varFolder In colSubfolders
strFolder = varFolder
Call FilesUsingDir(strFolder, FileList)
Next varFolder
End If

End Sub

You'd call this routine using code similar to:

Public Sub dirTest()
Dim colFileList As Collection
Dim intLoop As Integer
Dim strStartDir As String
Dim strMessage As String

strStartDir = "C:\"

Set colFileList = New Collection
Call ListFilesUsingDir( _
strStartDir, colFileList)
If colFileList.Count = 1 Then
strMessage = "There is 1 file under "
Else
strMessage = "There are " & _
colFileList.Count & " files under "
End If
strMessage = strMessage & strStartDir
Debug.Print strMessage
For intLoop = 1 To colFileList.Count
Debug.Print colFileList(intLoop)
Next intLoop
Debug.Print
Set colFileList = Nothing

End Sub

QualifyFolderPath, referred to in ListFilesUsingDir above, is simply a
helper function I use to ensure that the folder name ends in a slash:

Function QualifyFolderPath(PathName As String) _
As String

If Len(PathName) > 0 Then
If Right$(PathName, 1) <> "\" Then
QualifyFolderPath = PathName & "\"
Else
QualifyFolderPath = PathName
End If
Else
QualifyFolderPath = ""
End If

End Function
 
S

SusanV

Wow, thanks Doug! Now to wrap my brain around whatall the code is doing
<grin>

You rock!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top