Dir() command, scanning & addding file name to table

Faction21

Registered User.
Local time
Today, 08:38
Joined
Oct 26, 2004
Messages
42
I need to scan a directory and add file names in that directory to a table.

I was told that the Dir() command can scan for file names and you can use ADO/DAO code to insert them into your table. But, I need some guidance on excatly how to do this. Can anyone post some examples on how to do this?

Here is some specific information on what type of data I'm working on:
The file names are in a folder called Box 001
The files are PDF's with a bates number naming convention, such as
ACC 000001.pdf
ACC 000003.pdf
ACC 000005.pdf
ACC 000011.pdf
ACC 000012.pdf
ACC 000024.pdf

-Thanks guys for any help you can provide
 
This is getting me closer, but how exactly do I implement this? where or how do I use it to pull in the file names?

I put this code into a Module called GetFiles, but what do I do after this to get the file names? Or how or where do I call the function?


Option Compare Database

Sub GetFiles(strPath As String)
Dim rs As Recordset
Dim strFile As String, strDate As Date

'clear out existing data
CurrentDb.Execute "Delete * From tblDirectory", dbFailOnError

'open a recordset
Set rs = CurrentDb.OpenRecordset("tblDirectory", dbOpenDynaset)

'get the first filename
strFile = Dir(strPath, vbNormal)
'Loop through the balance of files
Do
'check to see if you have a filename
If strFile = "" Then
GoTo ExitHere
End If
strDate = FileDateTime(strPath & strFile)
rs.AddNew
'to save the full path using strPath & strFile
'save only the filename
rs!FileName = strFile
rs!FileDate = strDate
rs.Update

'try for next filename
strFile = Dir()
Loop

ExitHere:
Set rs = Nothing
MsgBox ("Directory list is complete.")
End Sub
 
Last edited:
Check this out for a working sample db... Browse [Find a directory or file]

The fScanDirSelectFileFromTable form updates the tFiles table with a listing of all files from a specific directory.

Look at the FilesAndDetails() function in the fScanDirSelectFileFromTable form for that is what you are after. You can alter the vDir = Dir(sPath & "*.*") line to filter out the files you want.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom