I've been tring to modify the last accessed file attribute on about 400,000 files.
The API reference includes SetFileTime - which looked good.
Searching the web for examples I found many, all using this call in the same way:
hFile = OpenFile (fName,...
SetFileTime hFile, newCreateTime, newAccessTime, newModifyTime
CloseFile hFile
Which only kinda works - I've modified the above to add msgboxes to follow progress, and what happens is that the SetFileTime can actually change the Last Accessed attribute, but the CloseFile "touches" the file and sets the Last Accessed attribute to the current system time.
The only way I've been able to acheive what I want is nasty - I change the system time, open the file, close the file, reset the system time.
Someone has suggested using FindFirstFile to get a handle to pass to SetFileTime, but I can't get this to work.
Anyone able to help me with this?
The best I've got so far is:
Private Sub Command1_Click()
'variables required
Dim hFile As Long
Dim fName As String
'structures required
Dim OFS As OFSTRUCT
Dim FT_CREATE As FILETIME
Dim FT_ACCESS As FILETIME
Dim FT_WRITE As FILETIME
'assign the textbox entry to the filename
fName = Text1
'open the file
hFile = OpenFile(fName, OFS, OF_READWRITE)
'get the FILETIME info for the created, accessed and last write info
Call GetFileTime(hFile, FT_CREATE, FT_ACCESS, FT_WRITE)
'set all times to last write time
Call SetFileTime(hFile, FT_WRITE, FT_WRITE, FT_WRITE)
'clean up by closing the file
Call CloseHandle(hFile)
End Sub
'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 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 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 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.wMinute = Minute(m_Date)
udtSystemTime.wSecond = Second(m_Date)
udtSystemTime.wMillisecond
' 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,
' 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
Instead of using the common dialog to get the file name, you could use the DIR() function to get the file name (or any other method of iterating the files that you are interested in). And pass this to a modified version of this sample.