Import Windows Explorer File List

  • Thread starter Thread starter gheagy
  • Start date Start date
G

gheagy

Is it possible to import a list of file names from Windows
Explorer into a table in Access?
 
Is it possible to import a list of file names from Windows
Explorer into a table in Access?

Not directly, no. But you can use Dir$ (check VBA help) to loop through all
files in a given folder, and for each item found, add a record to a table.

-- Dev
 
You can use the following:

However, BE CAREFULLY, as the resulting list can be quite large:

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

You could also modify the code to write out the file list to a table:

dim rstRecs as recordset

set rstRecs = currentdb.OpenRecordSet("tblFiles")

For i = 1 To dlist.Count
rstRecs.AddNew
rstRecs!FileName = dlist(i)
rstRecs.Update
next i
rstRecs.Close
set rstRecs = nothing
 
Back
Top