Folder and file listing into a table

  • Thread starter Thread starter SBD
  • Start date Start date
S

SBD

I'm looking for a way to read all sub folders and the
files within them into a table. The folders go 2-3 levels
deep. The table has the fields:

Folder
ModDate
ModTime
Type (either DIR or FILE)
Size
Filename

Anyone have or know of such a module?

Scott<>
 
You could use the follwing code..and modify it..

Sub dirTest()

Dim dlist As New Collection
Dim startDir As String
Dim i As Integer

startDir = "C:\access\"
Call FillDir(startDir, dlist)

MsgBox "there are " & dlist.Count & " in the dir"

' lets printout the stuff into debug window for a test

For i = 1 To dlist.Count
Debug.Print dlist(i)
Next i

End Sub


Sub FillDir(startDir As String, dlist As Collection)

' build up a list of files, and then
' add add to this list, any additinal
' folders

Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant

strTemp = Dir(startDir)

Do While strTemp <> ""
dlist.Add startDir & strTemp
strTemp = Dir
Loop

' now build a list of additional folders
strTemp = Dir(startDir & "*.", vbDirectory)

Do While strTemp <> ""
If (strTemp <> ".") And (strTemp <> "..") Then
colFolders.Add strTemp
End If
strTemp = Dir
Loop

' now process each folder (recursion)
For Each vFolderName In colFolders
Call FillDir(startDir & vFolderName & "\", dlist)
Next vFolderName

End Sub

With the above..you could modify the code example to add records to a
table...somting like:

Sub dirTestA()

Dim dlist As New Collection
Dim startDir As String
Dim i As Integer
dim rst as DAO.RecordSet

startDir = "C:\access\"
Call FillDir(startDir, dlist)

MsgBox "there are " & dlist.Count & " in the dir"

' add the list to a table...

set rst = currentdb.OpenRecordSet("tblFiles")

For i = 1 To dlist.Count
rst.AddNew
rst!FileName = dlist(i)
rst!LastModDate = FileDateTime(dlist(i))
rst!FileSize FileLen(dlist(i))
rst.Update
Next i

rst.Close

End Sub

The above would add each filename, last mod date..and file size to a table..
 
Back
Top