By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,334 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,334 IT Pros & Developers. It's quick & easy.

Get file attributes with code

P: n/a
Does anyone have any code snips that retrieves a file's (txt, xl, etc)
created, accessed, and modified date properties? I've searched this
group and couldn't find anything simple....

TIA

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Don't know what your definition of simple is, but I always use APIs like
Randy Birch illustrates at
http://vbnet.mvps.org/code/fileapi/filedatetime.htm

I suppose you could also use FileSystemObject, but I've always felt that
there was too much overhead to justify using it. Something like the
following untested air-code:

Sub ShowFileAccessInfo(filespec As String)
Dim fso As Object
Dim f As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
Debug.Print UCase(filespec)
Debug.Print "Created: " & f.DateCreated
Debug.Print "Last Accessed: " & f.DateLastAccessed
Debug.Print "Last Modified: " & f.DateLastModified
End Function

where filespec is the fully-qualified path to the file.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Soundneedle" <so*********@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Does anyone have any code snips that retrieves a file's (txt, xl, etc)
created, accessed, and modified date properties? I've searched this
group and couldn't find anything simple....

TIA

Nov 13 '05 #2

P: n/a
Thanks for the reply, Douglas.

I've been trying to adapt your air code to a command button's click
event....I do need some help though.

This is the code I'm trying:
------------------
Private Sub Command2_Click()
Dim filespec As String
Dim fso As Object
Dim f As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
Me.Text2 = f.DateCreated
Set fso = Nothing
Set f = Nothing
----------------

I get a runtime error: "Invalid Procedure Call or Argument" on
clicking. I've also tried placing your code into a class module as a
function....but, I'm not sure how to get a value returned to use for my
form's unbound text box.

Any suggestions?

Nov 13 '05 #3

P: n/a
Nevermind, I found some code from Terry Kreft:

Pass the filename and
0 for the creation date
1 for the last access date
2 for the last write date (modified)

**************************************************
'Code Start
**************************************************
Private Const OPEN_EXISTING = 3
Public Const INVALID_HANDLE_VALUE = -1

Private Const GET_CREATE_DATE = 0
Private Const GET_LAST_ACCESS_DATE = 1
Private Const GET_LAST_WRITE_DATE = 2

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Private Declare Function FileTimeToSystemTime Lib "kernel32" _
(lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" _
(lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function GetFileTime Lib "kernel32" (ByVal _
hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime _
As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA"
_
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As
Long, _
ByVal hTemplateFile As Long) As Long
Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal _
hFile As Long) As Long

Function FileTimeInfo(InFileName As String, WhichTime As Integer) As
Date
Dim dteRet As Date
Dim hFile As Integer
Dim CreationTime As FILETIME
Dim LastAccessTime As FILETIME
Dim LastWriteTime As FILETIME
Dim LocalTime As FILETIME
Dim SysTime As SYSTEMTIME

On Error GoTo FileTimeInfo_err
hFile = CreateFile(InFileName, 0&, 0&, 0&, OPEN_EXISTING, 0&, 0&)
If hFile = INVALID_HANDLE_VALUE Then
Err.Raise 1 + vbObjectError, Description:="Can't open the file"
Exit Function
End If

If GetFileTime(hFile, CreationTime, LastAccessTime, LastWriteTime) =
0
Then
Err.Raise 2 + vbObjectError, Description:="GetFileTime Failed"
End If

Select Case WhichTime
Case GET_CREATE_DATE
If FileTimeToLocalFileTime(CreationTime, LocalTime) = 0 Then
Err.Raise 2 + vbObjectError,
Description:="FileTimeToLocalFileTime
Failed"
End If
Case GET_LAST_ACCESS_DATE
If FileTimeToLocalFileTime(LastAccessTime, LocalTime) = 0 Then
Err.Raise 2 + vbObjectError,
Description:="FileTimeToLocalFileTime
Failed"
End If
Case GET_LAST_WRITE_DATE
If FileTimeToLocalFileTime(LastWriteTime, LocalTime) = 0 Then
Err.Raise 2 + vbObjectError,
Description:="FileTimeToLocalFileTime
Failed"
End If
End Select

If Not FileTimeToSystemTime(LocalTime, SysTime) Then
With SysTime
dteRet = DateSerial(.wYear, .wMonth, .wDay) + TimeSerial(.wHour,
..wMinute, .wSecond)
End With
Else
MsgBox "FileTimeToSystemTime Failed"
End If

FileTimeInfo_err:
If hFile <> INVALID_HANDLE_VALUE Then Call lclose(hFile)
FileTimeInfo = dteRet
Exit Function
FileTimeInfo_end:
With Err
Call MsgBox(.Description, vbExclamation + vbOKOnly, .Number)
End With
Resume FileTimeInfo_err
End Function
**************************************************
'Code End
**************************************************

Nov 13 '05 #4

P: n/a
You haven't defined filespec anywhere: it needs to be the full path to the
file.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Soundneedle" <so*********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks for the reply, Douglas.

I've been trying to adapt your air code to a command button's click
event....I do need some help though.

This is the code I'm trying:
------------------
Private Sub Command2_Click()
Dim filespec As String
Dim fso As Object
Dim f As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
Me.Text2 = f.DateCreated
Set fso = Nothing
Set f = Nothing
----------------

I get a runtime error: "Invalid Procedure Call or Argument" on
clicking. I've also tried placing your code into a class module as a
function....but, I'm not sure how to get a value returned to use for my
form's unbound text box.

Any suggestions?

Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.