Solved

Getting File Creation Date

Posted on 2002-03-21
31
427 Views
Last Modified: 2010-05-02
I have 4 files that I want to update in my C:\winnt\system32 directory.  The file dates of the old files are 1/20/2000 and the new file dates are 1/20/2002.  There are only 4 files to replace, but I want only replace these files with the new dated files.  How can I test for date on the file?
0
Comment
Question by:mtbuford
  • 11
  • 8
  • 5
  • +4
31 Comments
 
LVL 1

Expert Comment

by:alaplume
Comment Utility
something like this:

Add a reference the Microsoft Scripting runtime

dim o as filesystemobject
set o=new filesystemobject
dim ofile as file

set oFile=o.getfile "c:\test.txt"
oFile.createddate
0
 

Expert Comment

by:tanvirs
Comment Utility
   Dim objFSO As New FileSystemObject
    Dim objFile As File
   
    If objFSO.FileExists(txtExcelFile) Then
        Set objFile = objFSO.GetFile(txtExcelFile)
        MsgBox "Last Modified Date : " & objFile.DateLastModified & vbCrLf & " Date Created : " & objFile.DateCreated
        ' write code here, what ever you want to do.....
    End If
0
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
This is a little more cumbersome, but doesn't require a reference to the scripting runtime, which might save you from redistributing those dlls...

Option Explicit
Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * MAX_PATH
        cAlternate As String * 14
End Type
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long

Public Function GetFileSize(ByVal sFname As String) As Double

  Dim FData As WIN32_FIND_DATA
  Dim fHand As Long

  fHand = FindFirstFile(sFname , FData)
  if fHand < 1 then
     GetFileSize = 0
     Exit Function
  End if
  GetFileSize =  FData.nFileSizeLow
  fHand = FindClose(fHand)
 

End Function
0
 
LVL 18

Accepted Solution

by:
mdougan earned 50 total points
Comment Utility
Opps... you wanted the time:

Option Explicit

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 Type FILETIME
       dwLowDateTime As Long
       dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
       dwFileAttributes As Long
       ftCreationTime As FILETIME
       ftLastAccessTime As FILETIME
       ftLastWriteTime As FILETIME
       nFileSizeHigh As Long
       nFileSizeLow As Long
       dwReserved0 As Long
       dwReserved1 As Long
       cFileName As String * MAX_PATH
       cAlternate As String * 14
End Type

Declare Function FileTimeToSystemTime Lib "kernel32.dll" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String,
lpFindFileData As WIN32_FIND_DATA) As Long

Public Function GetFileCreatedDate(ByVal sFname As String) As String

 Dim FData As WIN32_FIND_DATA
 Dim fHand As Long
 Dim tSysTime as SYSTEMTIME
 Dim retval as long

 fHand = FindFirstFile(sFname , FData)
 if fHand < 1 then
    GetFileCreatedDate= ""
    Exit Function
 End if
 retval = FileTimeToSystemTime(FData.ftCreationTime, tSysTime)

 GetFileCreatedDate =  tSysTime.wMonth & "-" & tSysTime.wDay & "-" & tSysTime.wYear

 fHand = FindClose(fHand)

End Function
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
(From MS Help)

FileDateTime Function

Returns a Variant (Date) that indicates the date and time when a file was created or last modified.

Syntax

FileDateTime(pathname)

0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
rspahitz

The FileDateTime returns the date modified. If the file was never modified than yes it will be the same as the date created.

But I think you knew that already ... <g>

Anthony
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
Greetings.

This question has been locked with a Proposed Answer, and remains open today.

If the Proposed Answer did not serve your needs, please reject it and comment with an update.  If the Proposed Answer helped you, please accept it to grade and close this question.  If you need help splitting points between multiple experts, please comment here with details so we can help you.

EXPERTS ->  Please guide me here in terms of closing recommendations if the Asker does not respond in 4 days.

Thanks to all,
Moondancer - EE Moderator
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
mdougan's solution is the best and most complete.

Anthony
0
 
LVL 1

Expert Comment

by:kodiakbear
Comment Utility
Dear tanvirs
I've rejected your proposed answer as Experts Exchange holds an experiment to work without the answer button.

See: <http://www.experts-exchange.com/jsp/communityNews.jsp>
Paragraph: Site Update for Wednesday, November 06, 2002

By this rejection the Asker will be notified by mail and hopefully he will take his responsibility to finalize the question or post an additional comment.
The Asker sees a button beside every post which says "Accept This Comment As Answer" (including rejected answers) -- so if he/she thinks yours is the best, you'll be awarded the points and the grade.

EXPERTS: I will return in seven days to close this question.
Please leave your thoughts and recommendations here

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Thanks !

kodiakbear
Experts Exchange Moderator
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I already made my recommendation back in June.

Anthony
0
 
LVL 1

Expert Comment

by:kodiakbear
Comment Utility
acperkins,
Sorry for the confusion, this was unlocked with an automated program. When Moondancer left back in June we did not get a list of the Q's being worked on by her.
I will follow up in a few days and close this question.
(answer by mdougan)

kb
Experts Exchange Moderator (not automated :)  )
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
No problem.  I did not realize it was an automated program, I just thought you liked typing the same message over and over again <g>

Anthony
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
Although a recommendation was made for mdougan, I'd like to offer that my simple solution will probably be best in this case.

True that the filedatetime function returned the modified date, but when you're dealing with files "in my C:\winnt\system32 directory" these will most likely never be modified after creation, meaning that filedatetime will return the correct results in most or all cases.

Recommendation: points to rspahitz, mainly because of its simplicity.
0
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
Two comments....

rspahitz, you're assuming that the files in the system32 directory are windows system files, in which case I'd agree - update date and create date are possibly the same, but the question did not specify that.  In fact, because mtbuford wanted to replace only 4 specific files, I'd suggest that they are not windows files, but more likely application files installed in the system32 directory.  Either way, last modified date may not be what the questioner wanted.  The API gives you the option of looking at either modified or created date/time.

One reason that I recommended the much more complex API is that this operates on system files whether or not they are in use.  Some of the intrinsic functions are unable to get data about a file that is in use.  I don't know if that applies to FileDateTime or not, but I vaguely remember that there was this additional reason for not using one of the intrinsic functions.

Second comment... Moondancer was a she?  Who knew?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I agree with rspahitz, for the most part the FileDateTime is good enough.  I just ran a comparison between the two approaches, on my C:\winnt\system32\ folder.  Here are my results:

Allowing for the fact that the FileDateTime is local time (not UTC as is the API) and does not include milliseconds approximately 80% give the same result.  If I filter to only include DLL's the results are pretty much the same.

Anthony
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
So in conclusion, if 80% accuracy is good enough for you, go with FileDateTime otherwise you will have to use the API.

Anthony
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
m, although I agree with your reasoning, I still think that any user asking such a question is probably looking to rename a system file.  After examination of my system32 directory, I see the following:

#cnt Extension
   7 acm (?)
  51 ocx (x)
 268 exe (x)
1005 dll (x)
  21 ax  (?)
   1 bin (x)
   3 bmp (+)
   1 crl (?)
   1 cfg (+)
   7 cnt (+)
  14 ini (+)
  21 cpl (?)
  18 oca (x)
   1 cpi (?)
   2 cpx (?)
   9 dat (x)
  29 dep (x)
   3 deu (?)
  18 drv (x)
   1 eng (?)
   3 enu (?)
   3 esn (?)
   1 cov (?)
   1 (none)
   2 fnt (x)
   3 fra (?)
   2 gid (x)
   2 gif (+)
   9 h   (+)
  20 hlp (x)
   1 hxx (?)
   1 htt (?)
   1 ime (?)
   3 ita (?)
   1 jpn (?)
   1 lex (x)
   1 mdw (?)
  18 msc (?)
   1 mod (?)
  14 com (x)
   3 nld (?)
  51 nls (x)
   2 nt  (+)
   4 olb (x)
   1 pro (?)
   1 ram (?)
   1 rat (?)
   1 reg (?)
   3 rom (?)
   1 rsp (?)
  12 scr (x)
   4 sep (?)
  13 inf (+)
   1 sql (?)
  26 srg (?)
   3 sve (?)
  17 sys (x)
   4 txt (+)
  11 tlb (x)
   1 tmp (?)
   1 trm (?)
   4 trn (x)
   1 tsk (x)
   6 tsp (?)
   1 twd (?)
   8 uce (?)
   1 vbs (+)
   1 vxd (x)
   1 386 (x)
   1 (none)
   1 msi (x)
   1 cmd (?)

(x) means that this should not be modified
(+) means that this can be modified
(?) means that I have no idea what this is

If I count correctly, only 56 files might be modified out of 1783. 1541 should not be modified.  The rest would seem unlikely to be modified if you don't know what they are.

My guess is that user is trying to update some dlls and simply wants to know the creation date, which is most likely the same as the modified date unless he created them himself.  Even so, the modified date would probably suffice.

--
What say we split the points since we really can't tell what the user wanted.  I'd normally give them to you, but the complexity of your solution versus the simplicity of mine seems to balance the accuracy of yours and the unsureness of mine.
0
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
Well, obviously, I'm just having fun here, and am perfectly happy if they give rspahitz the points, give me the points or split them.

However, on technical grounds I still have to disagree.  The question was about getting the create date of the file, and even if 80% of the time the create date is the same as the last modified date, there is 20% of the time that it is inaccurate.  That's a higher rate of inaccuracy than I'd ever permit in one of my applications.  Now, maybe as you said, that would be enough for mtbuford, but we'll probably never know.

Also, my solution is as simple as yours to use.... in the code that mtbuford has to write, they simply call:

Dim sCreatedDate as String
sCreatedDate = GetFileCreatedDate("C:\WINNT\System32\kernel32.exe")

same as calling the FileDateTime function.  I've just provided the implementation of the function, which needs to be copy/pasted to a bas file.... no one even needs to look at that ugly code, it's already complete ;)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
mdougan,

Maybe I am stating the obvious, but I trust everybody realized that my remark about 80% accuracy was tongue in cheek.  If my code worked 80% of the time, I had better find another job!

>>no one even needs to look at that ugly code, it's already complete ;) <<
I agreed with you that the code you posted was the best solution, however there is that little detail about not declaring the constant MAXPATH, not to mention the fact that you are just included the date not the time.  I know I am being picky, but you knew that already <g>

Here is the revised code I used to compare the API approach to FileDateTime:

Private Function GetFileCreatedDate(ByVal sFname As String) As Variant
Dim FData As WIN32_FIND_DATA
Dim fHand As Long
Dim tSysTime As SYSTEMTIME
Dim retval As Long

fHand = FindFirstFile(sFname, FData)
If fHand < 1 Then
   GetFileCreatedDate = Null
   Exit Function
End If
retval = FileTimeToSystemTime(FData.ftCreationTime, tSysTime)

fHand = FindClose(fHand)

GetFileCreatedDate = DateSerial(tSysTime.wYear, tSysTime.wMonth, tSysTime.wDay) + _
                                TimeSerial(tSysTime.wHour - 6, tSysTime.wMinute, tSysTime.wSecond)
End Function

Anthony
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
>I've just provided the implementation of the function, which needs to be copy/pasted to a bas file.... no one even needs to look at that ugly code, it's already complete ;)

And that's true as long as the requirements never force the code to be maintained.  I suppose that the easy way is to simply throw it into a dll so that the code is isolated from the developer, then it never becomes a problem--maybe.

But, in the same way that you could write a super-terrific routine to validate user input, ready to be copied and pasted, it still becomes a potential issue.  Why do that when you have something that will work for you(*).  This is why I try to write my code to support the up-and-coming version of products.  For example, I never refer to the text property of a textbox by default (txtMyField) since that will break in .net...(it has to be txtMyField.text)  In the same way, Windows 3.1 functions (dlls) broke when run in Windows 95 because of the 16-bit to 32-bit issue, but VB commands that did the same function worked perfectly.  I don't like to risk such things if they can be avoided, so if I have a VB command versus an API call I'll almost always use the VB command.  This case goes beyond that so that's where you have to weight the differences (*)....

(*) Since 80% is really meaningless in the current question, I'd argue back that if both of our examples worked 100% of the time, why insert the extra code--just use the simple solution of one line of code.  Since there are only four files in question, if any one of them failed with my suggestion, then it's a HORRIBLE choice.  If the set of files will be expanded in the future, then I'd probably go with your choice just to be safe because when it comes to replacing files, you can't afford one mistake.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>Since 80% is really meaningless in the current question<<
Excuse me?  20% of the DLL's on my box had a create date different to a modified date.  If the date created was the one needed, why is that meaningless?

I wonder if mtbuford thinks he/she is getting spammed?

Anthony
0
 
LVL 18

Expert Comment

by:mdougan
Comment Utility
<g>

Well, in any event, I wish they'd close the question already.  I'd like to file it away in my archive, as there is some good info here.

0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
>If the date created was the one needed

Exactly!

In this case, percentage of failure means nothing.  Only actual numbers matter here.  If all four needed files have the same create and mod date, then 100% match; if not then whatever the percentage is potential failure.  Even if your number was 4% the same (assuming 100 files) then that would be good enough if those were the four files that were needed; likewise, even if it were 99%, if that one file was needed, it would be a failure.

That's why the % is meaningless.

0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
>I wish they'd close the question already

OK.  Here's my recommendation:

Split points between mdougan and rspahitz.

--
If there are no objections, then I guess we're done.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>> That's why the % is meaningless.<<
Now I see what you mean.  <walks away dazed and confused>

Anthony
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
My reommendation is that acperkins should get the points.

Ok, I waffled.
Anthony
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
Hmm...well, you did supply the corrected code in a single comment...but--?

I stick with my recommendation, since user NEVER commented and we really have no idea which option is easiest to implement/best for the given request.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
rspahitz,

I was only kidding.

Anthony
0
 
LVL 1

Expert Comment

by:kodiakbear
Comment Utility
Guys,
LOL you think only mtbuford is the only one thinking he is getting spammed LOL. There has been more activity on this question today alone than the entire 8 months since it was asked.
I had already changed my decision as to the outcome of this question so you can relax. I don't have time at this exact moment(have to run to son's band concert) but it is going to be a split. (If only to get the email traffic down LOL)
Seriously I will split later tonight.
Now I know this will generate more debate, who to award the Q to and who gets the points for :)

kb
Experts Exchange Moderator
0
 
LVL 1

Expert Comment

by:kodiakbear
Comment Utility
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
I thought our debate was done until you said, "who to award the Q to and who gets the points for"  LOL!  Thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now