473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 15585
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 FileInfo.Attributes or File.SetAttributes. It doesn't work for...
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 windows explorer.
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 2000 Domain Controller all service packs and...
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 and if a correspondent node in my xml config 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 and if a correspondent node in my xml config file...
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 with danger and only trusted users should be...
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 update it if neccessary... Here is my problem :...
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 (install dir., file names, passwords, ect.) and...
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 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.