• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3151
  • Last Modified:

Add / Subtract File times

When using GetFileTime:

Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long

How do I add or subtract values to the file time to find out how old the file is? For instance, suppose I wanted to list all the files that were more than 5 days old, how would I do it?
0
DrDamnit
Asked:
DrDamnit
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Hi DrDamnit,

You can just do [One Date] - [Another Date] to subract two dates; the result will be a decimal value where 1 = each day.  So in your example...

Dim dOneFile as date, dCurrentDate as date

'Get the dOneFile date using your API call
dCurrentDate = Now

If dCurrentDate - dOneFile > 5 then
    'File is older than 5 days old.  Do what you want here.
End If

Hope this helps.
-Jim
0
 
anthonywjones66Commented:
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


Declare Function FileTimeToSystemTime Lib "kernel32" Alias "FileTimeToSystemTime" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long

these help? :)
0
 
mladenoviczCommented:
Check FileTimeToSystemTime  API, it converts FILETIME to SYSTEMTIME

You can use DateSerial to convert SYSTEMTIME to Date.

In order to compare dates you should use DateDiff function
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DrDamnitAuthor Commented:
I am not using a date type.

As Per MSDN:
It is not recommended that you add and subtract values from the FILETIME structure to obtain relative times. Instead, you should

    * Copy the resulting FILETIME structure to a ULARGE_INTEGER structure.
    * Use normal 64-bit arithmetic on the ULARGE_INTEGER value.

How do I do that?
0
 
mladenoviczCommented:
Ok. Then try CompareFileTime API

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 SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CompareFileTime Lib "kernel32" (lpFileTime1 As FILETIME, lpFileTime2 As FILETIME) As Long
Private Sub Form_Load()
    'KPD-Team 2001
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim SysTime As SYSTEMTIME, FT1 As FILETIME, FT2 As FILETIME
    Dim Ret As Long
    'initialize SYSTEMTIME structure
    With SysTime
        .wMilliseconds = 300
        .wSecond = 9
        .wMinute = 59
        .wHour = 17
        .wMonth = 5
        .wYear = 2001
        .wDay = 21
        .wDayOfWeek = 1
    End With
    'convert SYSTEMTIME to FILETIME
    SystemTimeToFileTime SysTime, FT1
    'adjust the SYSTEMTIME structure
    SysTime.wMinute = SysTime.wMinute - 10
    'convert SYSTEMTIME to FILETIME
    SystemTimeToFileTime SysTime, FT2
    'compare the two FILETIMEs
    Ret = CompareFileTime(FT1, FT2)
    'show the result
    Select Case Ret
        Case -1
            MsgBox "First file time is less than second file time.", vbInformation
        Case 0
            MsgBox "First file time is equal to second file time.", vbInformation
        Case 1
            MsgBox "First file time is greater than second file time.", vbInformation
    End Select
End Sub
0
 
DrDamnitAuthor Commented:
This is on the right track of what I want, but I am not sure how to implement it:
http://support.microsoft.com/?kbid=189862

For instance, on Linux, a timestamp is display as the number of seconds that have elapsed since 1/1/1970. So, if I want to know if timestamp x is more than a week old (from the current time), I can just subtract 604800 from the current timestamp. Then, if the file is outside that range, it is too old. If it is within the range, it is not too old.

That's kind of what I want to do.
0
 
anthonywjones66Commented:
Try this:-

Const mccurDateBase As Currency = 9435312000000#
Const mclMilliSecsPerDay = 86400000

Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As Currency, lpLastAccessTime As Currency, lpLastWriteTime As Currency) As Long

Note changed to currency then:-

Dim datLastWriteTime as Date

datLastWriteTime = (lpLastWriteTime - mccurDateBase) / mclMilliSecsPerDay

If (Now - datLastWriteTime) > 5 Then
'older than 5 days
End If

Anthony.
0
 
DrDamnitAuthor Commented:
Closer...except lpLastAccessTime is actually of this type:

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

which is two 32-bit numbers to compose a 64-bit structure. How would I handle that?
0
 
anthonywjones66Commented:
DrDamnit,

>>
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

which is two 32-bit numbers to compose a 64-bit structure. How would I handle that?
<<

Don't. Treat it as Currency by changing the API declare then with a little y = Ax + B transform you can assign the results to standard VB Date variable.

See my code above.

Anthony.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now