Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / January 2007

Tip: Looking for answers? Try searching our database.

Search Directory Structure in Access VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
joseph micheli - 23 Jan 2007 03:20 GMT
I would like to search a directory structure for all .mdb files and write the
file path of each one to a table.  I want to start at the highest level of
the drive and search all subdirectories without specifying the subdirectory
names.  Does anyone have any suggestions?

Thanks.
Signature

Joe Micheli CPA, CMA, CFM, CPIM

Albert D. Kallal - 23 Jan 2007 05:43 GMT
This code will work...

Sub dirTest()

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

  startDir = "C:\Documents and Settings\Albert\My Documents\"
  strType = "*.mdb"

  Call FillDir(startDir, dlist, strType)

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

  ' lets printout the stuff into debug window for a test

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

End Sub

Sub FillDir(startDir As String, dlist As Collection, sType As String)

  ' 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 & sType)

  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 GetAttr(startDir & strTemp) = vbDirectory Then
        If (strTemp <> "..") And (strTemp <> ".") Then
           colFolders.Add strTemp
        End If
     End If
     strTemp = Dir
  Loop

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

End Sub

Of couse, you need to change:

  For i = 1 To dlist.Count
     Debug.Print dlist(i)
  Next i
to;
    dim rstData           as dao.recordset
     set rstData = currentdb.OpenRecordSet("tblFiles")
    for i = 1 to dlist.count
       rstData.Addnew
       rstData!MyFile = dlist(i)
       rstData.Update
   next i
rstData.Close

The above example uses recursion...and will "walk" all directories below the
one you start.....

Signature

Albert D. Kallal    (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@msn.com

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.