Reading files from a folder and loading atributes into a database.

F

FatMan

Hi all:
I need to know how to read the files in a windows folder and load the file
atributes (fiilename, size, date last modified, date created, etc.) into a
database. Is this possible to do and if so how can it be done? I am working
with Access 2000 and Win XP Pro.

Any help is greatly appreaciated.

Thanks,
FatMan
 
K

kc-mass

Try something like this:

Public Sub FilePick
Dim fDialog As Office.FileDialog
Dim strControl As String
Dim strForm As String
Dim varFile As Variant
Dim strSelection As String
Dim strExtension As String
Dim strPath As String
Dim strFileName As String
Dim intFileMark As Integer
Dim intExtMark As Integer
'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Restrict user to one selection in dialog box
.AllowMultiSelect = True
'Set the title of the dialog box.
.Title = "Please Select the Files"
'Show the dialog box. If the .Show method returns True, the
'user picked a file - else cancelled.
If .Show = True Then
'Loop through each file selected and add it to our list box.
For Each varFile In .SelectedItems
strControl = "txtLoaded" & FileNumber
'Debug.Print Len(CStr(varFile)); "varfile"

strSelection = CStr(varFile)
Forms(strForm)(strControl) = Trim(strSelection)
intExtMark = InStr(strSelection, ".") + 1
intFileMark = Len(strSelection) - InStrRev(strSelection, "\")
strPath = Left(strSelection, InStrRev(strSelection, "\"))
strExtension = Mid(strSelection, intExtMark)
strFileName = Right(strSelection, intFileMark)
Next
Else
MsgBox "File selection process cancelled."
Exit Sub
End If
End With
End Sub



This was to pick a file but you will understand the changes needed.

Regards

Kevin
 
F

FatMan

Kevin:
Thank you for your reply.

If I understand your code correctly you make use of a dialog box to select a
file/files. My version of Access 2000 is giving me a "Compile errror:
User-defined type not defined" error on the line of code "Dim fDialog As
Office.FileDialog". I really don't need a dialog box and could hard code the
path to the folder.

What I really want to do is to read each file in a folder and place the
values for its attributes (file name, size date created, date last modified,
etc.) into a database.

Is there anyting you or anyone else can do to help me. The file type to be
read are text (txt) files but I am not interested in the contents just the
atributes of the file.

Thanks,
FatMan
 
J

Jack Leach

I think you are looking for file properties, rather than attributes. Use
GetAttr() to get file attributes (readonly, archived, hidden, etc).

The code below reads file properties (name, type, date created, modified,
accessed, etc). This should be what you are looking for.

The original link (referenced below) was for reading jpg files, but if I
understand correctly, all files have the same number of properties, it's just
a matter of whether they have information in them.

The function FileGetProperties returns an array of all the properties (a
string, ex "Name: somefile.jpg". See the TestProps function for an idea how
to implement. This particular examples pulls returns the 3rd element of a
Base 0 array. You may need to loop the array until you get the required
element.

I have in mind to further break this down and enumerate the possible
properties so a function may return only the specified rather than all
properties, but I need to test this on various filetypes to see what property
names etc change and which are constant (if any) between files.

In the meantime, though, this seems to work well.




Public Function TestProps() As String
Dim ret As Variant
ret = FileGetProperties("D:\something.jpg")
TestProps = ret(2)
End Function


'==============================================================================
' FileGetProperties
'
' Adapted (copied) from Michael Pierron
' http://www.pcreview.co.uk/forums/thread-1862574.php
'-----------------------
Public Function FileGetProperties(sfile As String) As Variant
On Error GoTo Err_Proc
Dim ret(34) As String
'==================
Dim i As Integer
Dim T As String
'==================

With CreateObject("Shell.Application").Namespace(Left( _
sfile, lPosition(sfile, "\") - 1))

For i = 0 To 34
T = .GetDetailsOf(.parsename(Dir(sfile)), i)
If Len(T) Then
ret(i) = .GetDetailsOf(.Items, i) & ":" & T
Debug.Print ret(i)
End If

Next i

End With

'==================
Exit_Proc:
FileGetProperties = ret
Exit Function
Err_Proc:
If cSYSDISPERR Then MsgBox "Error!" & vbCrLf & _
Str(Err.Number) & ": " & Err.Description, _
vbCritical, "Library Error!"
Resume Exit_Proc
Resume
End Function


Private Function lPosition%(Chain$, Char$)
'Dependant of FileGetProperties
Dim iPos%
Do
iPos = InStr(lPosition + 1, Chain, Char, 1)
If iPos Then lPosition = iPos Else Exit Do
Loop
End Function


--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
K

kc-mass

Hi,

If you don't need the dialog try something like this:

Sub FileNames()
Dim strFileName As String
FileName = Dir("C:\*.txt")
Do While FileName <> ""
Debug.Print strFileName
'Get the file properties in here and store them in your database
table.
strFileName = Dir()
Loop
End Sub

Regards

Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top