Sub StoreFilesToTableA()
Dim fso As FileSystemObject
Dim folder As folder
Dim file As file
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strFolderPath As String
' 探索するフォルダのパス
strFolderPath = "C:\YourFolderPath"
Set fso = New FileSystemObject
Set db = CurrentDb
Set rst = db.OpenRecordset("TableA", dbOpenTable)
' フォルダおよびサブフォルダ内のファイルを取得しテーブルに格納
Call RecursiveFiles(fso.GetFolder(strFolderPath), fso, rst)
' オブジェクトを解放
rst.Close
Set rst = Nothing
Set db = Nothing
Set fso = Nothing
End Sub
' 再帰的にファイルを取得するためのサブルーチン
Sub RecursiveFiles(folder As folder, fso As FileSystemObject, rst As DAO.Recordset)
Dim subFolder As folder
Dim file As file
' フォルダ内の全てのファイルを処理
For Each file In folder.Files
rst.AddNew
rst!ファイル名 = file.Name
rst!フルパス = file.Path
rst.Update
Next file
' サブフォルダを再帰的に処理
For Each subFolder In folder.SubFolders
Call RecursiveFiles(subFolder, fso, rst)
Next subFolder
End Sub
コメント