Results 1 to 4 of 4

Thread: System Time to File Time

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    250

    System Time to File Time

    I am trying to reset a file's times to some date during the year 1980, converting system time to file time. The only problem is that I have problems setting up correct values in the variable so the API works. Someone help me please.

    'The API:

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

    'The datatypes:

    Public Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
    End Type

    Public 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

    'For more information go to
    'http://www.allapi.net/api/SystemTimeToFileTime.php
    Why does everyone think I may be dangerous? I'm just good at computers.

  2. #2
    Matthew Gates
    Guest
    See if this example from the API-Guide helps any.


    VB Code:
    1. 'This project needs a Common Dialog box, named CDBox.
    2. '  (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
    3. '   and select Microsoft Common Dialog control)
    4.  
    5. Private Declare Function CreateFile Lib "kernel32" _
    6. Alias "CreateFileA" (ByVal lpFileName As String, ByVal _
    7. dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal _
    8. lpSecurityAttributes As Long, ByVal dwCreationDisposition As _
    9. Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile _
    10. As Long) As Long
    11.  
    12. Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As _
    13. Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, _
    14. lpLastWriteTime As FILETIME) As Long
    15.  
    16. Private Declare Function SystemTimeToFileTime Lib "kernel32" _
    17. (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
    18.  
    19. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    20. hObject As Long) As Long
    21.  
    22. Private Declare Function LocalFileTimeToFileTime Lib "kernel32" _
    23. (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
    24.  
    25. Private Type FILETIME
    26.     dwLowDateTime As Long
    27.     dwHighDateTime As Long
    28. End Type
    29.  
    30. Private Type SYSTEMTIME
    31.     wYear As Integer
    32.     wMonth As Integer
    33.     wDayOfWeek As Integer
    34.     wDay As Integer
    35.     wHour As Integer
    36.     wMinute As Integer
    37.     wSecond As Integer
    38.     wMilliseconds As Integer
    39. End Type
    40.  
    41. Private Const GENERIC_WRITE = &H40000000
    42. Private Const OPEN_EXISTING = 3
    43. Private Const FILE_SHARE_READ = &H1
    44. Private Const FILE_SHARE_WRITE = &H2
    45.  
    46.  
    47. Private Sub Form_Load()
    48.     'KPD-Team 1998
    49.     'URL: [url]http://www.allapi.net/[/url]
    50.     Dim m_Date As Date, lngHandle As Long
    51.     Dim udtFileTime As FILETIME
    52.     Dim udtLocalTime As FILETIME
    53.     Dim udtSystemTime As SYSTEMTIME
    54.     m_Date = Format(Now, "DD-MM-YY")
    55.  
    56.     'Set the dialog's title
    57.     CDBox.DialogTitle = "Choose a file ..."
    58.     'Set the dialog's filter
    59.     CDBox.Filter = "All Files (*.*)|*.*"
    60.     'Show the 'Open File'-dialog
    61.     CDBox.ShowOpen
    62.  
    63.     udtSystemTime.wYear = Year(m_Date)
    64.     udtSystemTime.wMonth = Month(m_Date)
    65.     udtSystemTime.wDay = Day(m_Date)
    66.     udtSystemTime.wDayOfWeek = WeekDay(m_Date) - 1
    67.     udtSystemTime.wHour = Hour(m_Date)
    68.     udtSystemTime.wSecond = Second(m_Date)
    69.     udtSystemTime.wMilliseconds = 0
    70.  
    71.     ' convert system time to local time
    72.     SystemTimeToFileTime udtSystemTime, udtLocalTime
    73.     ' convert local time to GMT
    74.     LocalFileTimeToFileTime udtLocalTime, udtFileTime
    75.     ' open the file to get the filehandle
    76.     lngHandle = CreateFile(CDBox.Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
    77.     ' change date/time property of the file
    78.     SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
    79.     ' close the handle
    80.     CloseHandle lngHandle
    81.     MsgBox "The date of the file '" & CDBox.Filename & "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
    82. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2001
    Posts
    250
    Sorry Mathew, it won't work. I'm tyring to set the date to a specific date, not a variable date equal to the current time. Your example wouldn't get an error, but I need to set the file time to a specific date.
    Why does everyone think I may be dangerous? I'm just good at computers.

  4. #4
    jim mcnamara
    Guest
    Try
    Code:
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    
    Dim myTime as FILETIME
    const durationDay = 7.15828
    const year = 365.24
    
    Function SubDays(hiLong as long, years as long) as long
           Dim i as long
           i = hiLong - Clng( Cdbl(years) * year * durationDay)
           SubDays = i
    
    End Function
    Usage: assuming you have a file with this year's date
    get FILETIME for the file in question
    years = 2001 - 1980
    myTime.dwHighDateTime = SubDays(dwHighDateTime, years)

    Use myTime in SetFileTime



    PS: the day const may be 3.57194 instead of 7.1....
    I don't have my notes on this and I can't remember. It's one or the other. Try and see.

    Als go here for more information on date arithmetic on files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width