|
I wrote a quick function that will recursively traverse a file system and store the files in Access database. For the code to work, you need to create a table with an attachment name fileobj.
Sub test() Dim c As New clsProfile c.startFunction "test" traverseDir "C:\Course Technology\" c.endFunction Set c = Nothing End Sub
Sub traverseDir(sPath As String) Dim c As New clsProfile c.startFunction "traverseDir" Dim sFile As String Dim db As Database Dim rs As Recordset Dim rsFile As Recordset2 Dim filenames() As String Dim fileCount As Integer ReDim filenames(1) Dim i As Integer fileCount = 1 Set db = CurrentDb Set rs = db.OpenRecordset("tblFiles") sFile = Dir(sPath & "*.*", vbDirectory) Do While Len(sFile) > 0 filenames(fileCount) = sFile
sFile = Dir() If sFile <> "" Then fileCount = fileCount + 1 ReDim Preserve filenames(fileCount) End If Loop For i = 1 To fileCount sFile = filenames(i) If sFile <> "." And sFile <> ".." And UCase(Right(sFile, 4)) <> ".MDB" Then If (GetAttr(sPath & sFile) And vbDirectory) = vbDirectory Then traverseDir sPath & sFile & "\" Else rs.AddNew rs("FileName") = sFile rs("dir") = sPath Set rsFile = rs.Fields("FileObj").Value rsFile.AddNew rsFile.Fields("filedata").LoadFromFile sPath & sFile rsFile.Update rsFile.Close rs.Update End If End If Next rs.Close c.endFunction Set c = Nothing End Sub
|