472,328 Members | 1,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Get file attributes with code

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
4 15409
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John R. | last post by:
How do you set the following file attributes: Compressed Encrypted Normal ReparsePoint SparsePoint You CAN'T set these using...
13
by: Alexandra | last post by:
Hi, I have a hidden system file that I need to read. I am logged in as an administrator. I can not change the file attributes using the...
7
by: Mark | last post by:
Hello, I have researched and tried every thing I have found on the web, in groups and MS KB articles. Here is what I have. I have a Windows...
10
by: JJ | last post by:
How can I get a SiteMapNodeCollection to output as an XML file?
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked...
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked...
3
acoder
by: acoder | last post by:
How to Upload a File in Coldfusion Use the cffile tag for uploading files to the server. Note that allowing people to upload files is fraught...
6
by: | last post by:
Hi, I'm steel trying to read and update my XML file with Visual Basic Express but i am unable to find the right way to read my xml file and...
1
by: nitusa | last post by:
Hi All, First time poster, and newbie C# programmer so be patient with my ignorance. :) For my current project I need to store some information...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.