Link to home
Start Free TrialLog in
Avatar of Bubba Jones
Bubba Jones

asked on

Import file listing into access database

I am hoping someone has some MS ACCESS VBA code that can point to a given file path, say, "H:\" , locate only .ZIP and build a table named PACKAGES (or overwrite the existing PACKAGES table) , and add the following fields:

1.  File Creation date
2.  File Creation Time
3   File Size
3.  File Name
SOLUTION
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

Here is a small routine that does what you described.  Also attaching a small database file with the defined table I used in there.  Hope this helps.

Option Compare Database
Option Explicit

Sub GetFiles()
    Dim strBaseDir As String
    Dim strExtension As String
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim rsFiles As Recordset
    
    strBaseDir = "c:\temp"
    strExtension = "zip"
    
    CurrentDb.Execute "Delete * From Packages", dbFailOnError
    Set rsFiles = CurrentDb.OpenRecordset("Packages", dbOpenDynaset)
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strBaseDir)
    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Path)) = LCase(strExtension) Then
            rsFiles.AddNew
            rsFiles!FileName = objFile.Name
            rsFiles!Filesize = objFile.Size
            rsFiles!Filedate = objFile.DateCreated
            rsFiles.Update
        End If
    Next
    
    Set rsFiles = Nothing
    
End Sub

Open in new window


EE29023723.accdb


»bp
Avatar of Bubba Jones

ASKER

Wow! Crystal and Bill, thanks for your suggestions, apologies, I was out of town, and tied up.

Bill - Is there a way to have your code look through sub-folders as well, and can it possibly accept a UNC path, vs a drive letter and colon ?

Crystal -  when trying LoopFilesAndStore("H:\", "*.zip") in your solution, I get a "COMPILE ERROR, EXPECTED ="  If I do not specify a file mask, it runs against the parent folder.  Is it also possible to do all sub folders, and a UNC ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial