KamiKazeKiwi3
Jun 8th, 2001, 03:58 PM
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
Matthew Gates
Jun 8th, 2001, 09:31 PM
See if this example from the API-Guide helps any.
'This project needs a Common Dialog box, named CDBox.
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
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 SetFileTime Lib "kernel32" (ByVal hFile As _
Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, _
lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" _
(lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" _
(lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
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 Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'KPDTeam@Allapi.net
Dim m_Date As Date, lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
m_Date = Format(Now, "DD-MM-YY")
'Set the dialog's title
CDBox.DialogTitle = "Choose a file ..."
'Set the dialog's filter
CDBox.Filter = "All Files (*.*)|*.*"
'Show the 'Open File'-dialog
CDBox.ShowOpen
udtSystemTime.wYear = Year(m_Date)
udtSystemTime.wMonth = Month(m_Date)
udtSystemTime.wDay = Day(m_Date)
udtSystemTime.wDayOfWeek = WeekDay(m_Date) - 1
udtSystemTime.wHour = Hour(m_Date)
udtSystemTime.wSecond = Second(m_Date)
udtSystemTime.wMilliseconds = 0
' convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
' convert local time to GMT
LocalFileTimeToFileTime udtLocalTime, udtFileTime
' open the file to get the filehandle
lngHandle = CreateFile(CDBox.Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
' change date/time property of the file
SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
' close the handle
CloseHandle lngHandle
MsgBox "The date of the file '" & CDBox.Filename & "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub
KamiKazeKiwi3
Jun 9th, 2001, 02:05 AM
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.
jim mcnamara
Jun 9th, 2001, 12:26 PM
Try
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
:rolleyes:
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 (http://support.microsoft.com/support/kb/articles/Q188/7/68.ASP) for more information on date arithmetic on files.