Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i access file attributes 2

Status
Not open for further replies.

coverdale

Technical User
Mar 11, 2000
5
CA
I'm trying to access the time a file was created. How can i get access to this attribute
 
This was handed to me recently. It isn't my code and I can't find the author... but it seems to work fairly well.<br>
<br>
Private Declare Function CreateFile Lib &quot;kernel32&quot; Alias &quot;CreateFileA&quot; (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal NoSecurity As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long<br>
Private Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject As Long) As Long<br>
Private Declare Function GetFileTime Lib &quot;kernel32&quot; (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long<br>
Private Type FILETIME<br>
&nbsp;&nbsp;&nbsp;&nbsp; dwLowDateTime As Long<br>
&nbsp;&nbsp;&nbsp;&nbsp; dwHighDateTime As Long<br>
End Type<br>
<br>
Private Const GENERIC_READ = &H80000000<br>
Private Const FILE_SHARE_READ = &H1<br>
Private Const FILE_SHARE_WRITE = &H2<br>
Private Const OPEN_EXISTING = 3<br>
<br>
Private Function FileTimeToDate(ft As FILETIME) As Date<br>
<br>
Const TICKS_PER_SECOND = 10000000<br>
<br>
Dim lo_time As Double<br>
Dim hi_time As Double<br>
Dim seconds As Double<br>
Dim hours As Double<br>
Dim the_date As Date<br>
<br>
If ft.dwLowDateTime &lt; 0 Then<br>
&nbsp;&nbsp;&nbsp;&nbsp; lo_time = 2 ^ 31 + (ft.dwLowDateTime And &H7FFFFFFF)<br>
Else<br>
&nbsp;&nbsp;&nbsp;&nbsp; lo_time = ft.dwLowDateTime<br>
End If<br>
<br>
If ft.dwHighDateTime &lt; 0 Then<br>
&nbsp;&nbsp;&nbsp;&nbsp; hi_time = 2 ^ 31 + (ft.dwHighDateTime And &H7FFFFFFF)<br>
Else<br>
&nbsp;&nbsp;&nbsp;&nbsp; hi_time = ft.dwHighDateTime<br>
End If<br>
<br>
seconds = (lo_time + 2 ^ 32 * hi_time) / TICKS_PER_SECOND<br>
hours = CLng(seconds / 3600)<br>
seconds = seconds - hours * 3600<br>
<br>
the_date = DateAdd(&quot;h&quot;, hours, &quot;1/1/1601 6:00 AM&quot;)<br>
the_date = DateAdd(&quot;s&quot;, seconds, the_date)<br>
FileTimeToDate = the_date<br>
End Function<br>
<br>
Private Function GetFileTimes(ByVal file_name As String, ByRef date_created As Date, ByRef date_accessed As Date, ByRef date_written As Date) As Boolean<br>
Dim file_handle As Long<br>
Dim creation_time As FILETIME<br>
Dim access_time As FILETIME<br>
Dim write_time As FILETIME<br>
<br>
file_handle = CreateFile(file_name, GENERIC_READ, _<br>
FILE_SHARE_READ Or FILE_SHARE_WRITE, _<br>
0&, OPEN_EXISTING, 0&, 0&)<br>
If file_handle = 0 Then<br>
&nbsp;&nbsp;&nbsp;&nbsp; GetFileTimes = True<br>
&nbsp;&nbsp;&nbsp;&nbsp; Exit Function<br>
End If<br>
<br>
If GetFileTime(file_handle, creation_time, _<br>
&nbsp;&nbsp;&nbsp;&nbsp; access_time, write_time) = 0 _<br>
Then<br>
&nbsp;&nbsp;&nbsp;&nbsp; GetFileTimes = True<br>
&nbsp;&nbsp;&nbsp;&nbsp; Exit Function<br>
End If<br>
<br>
If CloseHandle(file_handle) = 0 Then<br>
&nbsp;&nbsp;&nbsp;&nbsp; GetFileTimes = True<br>
&nbsp;&nbsp;&nbsp;&nbsp; Exit Function<br>
End If<br>
<br>
date_created = FileTimeToDate(creation_time)<br>
date_accessed = FileTimeToDate(access_time)<br>
date_written = FileTimeToDate(write_time)<br>
End Function<br>
<br>
Private Sub Command1_Click()<br>
Dim date_created As Date<br>
Dim date_accessed As Date<br>
Dim date_written As Date<br>
Dim txt As String<br>
<br>
If GetFileTimes(Text1.Text, date_created, date_accessed, date_written) Then<br>
&nbsp;&nbsp;&nbsp;&nbsp; lblTimes.Caption = &quot;&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp; MsgBox &quot;Error getting file times&quot;<br>
Else<br>
&nbsp;&nbsp;&nbsp;&nbsp; txt = &quot;Created:&quot; & vbCrLf & &quot; &quot; & Format$(date_created) & vbCrLf & _<br>
&nbsp;&nbsp;&nbsp;&nbsp; &quot;Last Accessed:&quot; & vbCrLf & &quot; &quot; & Format$(date_accessed) & vbCrLf & _<br>
&nbsp;&nbsp;&nbsp;&nbsp; &quot;Last Written:&quot; & vbCrLf & &quot; &quot; & Format$(date_written)<br>
&nbsp;&nbsp;&nbsp;&nbsp; lblTimes.Caption = txt<br>
End If<br>
End Sub<br>
<br>
Private Sub Form_Load()<br>
Dim file_name As String<br>
<br>
file_name = App.Path<br>
If Right$(file_name, 1) &lt;&gt; &quot;\&quot; Then file_name = file_name & &quot;\&quot;<br>
file_name = file_name & &quot;Form1.frm&quot;<br>
Text1 = file_name<br>
End Sub<br>
<br>
<br>
<p> <br><a href=mailto: > </a><br><a href= Vorpalcom home page</a><br>Send me suggestions or comments on my current software project.
 
coverdale<br>
There's a function FileDateTime() in VB<br>
--Jim
 
Sorry, Jim and Coverdale. The code was bit excessive but I thought I'd throw it on the forum because it showed &quot;last access time&quot; too. And I thought it was kinda neat.<br>
<br>
I'll stick to the easy solutions in the future.<br>
<p> <br><a href=mailto: > </a><br><a href= Vorpalcom home page</a><br>Send me suggestions or comments on my current software project.
 
Just to make things worse for you here Alt - the filesystemobject from the scripting library (vb6 - but works with 5) does all that and more...<br>
<br>
-ml<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
<br>
Mike,<br>
<br>
This thread is a great example of why threads shouldn't be closed -- we would miss the implied humor in our tech-world.<br>
<br>
But seriously folks... Coverdale may have closed the thread after seeing that Alt255's solution worked and missed out on a couple of good alternate solutions.<br>
<br>
good post!<br>
<br>
Tarek
 
OK! I Give In!<br>
<br>
&quot;Closing Threads&quot; is a Defunct Idea and something that other, lesser, sites do.<br>
<br>
&lt;wiping blood off&gt;....<br>
<br>
&lt;smile&gt;<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
So Mike, You're going to start archiving all these posts so they can be searched, right?<br>
<br>
&lt;gd&r&gt;<br>
<br>
Chip H.<br>

 
So Mike, You're going to start archiving all these posts so they can be searched, right?<br>
<br>
&lt;gd&r&gt;<br>
<br>
Chip H.<br>

 
I want to use this FileDateTime function on each file in a particular directory to return dates and do certain things dependent on the date.

Can anyone help me with the code to do this?

Cheers
Jo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top