VB and VBA Users Source Code: Changing a files created, modified and accessed times.
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Changing a files created, modified and accessed times.
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, March 13, 2001
Hits:
791
Category:
Windows API
Article:
The following code demonstrations how to alter the three file date stamps (creation, last accessed and last modified). Option Explicit Private Type FILETIME LowDateTime As Long HighDateTime As Long End Type Private Type SYSTEMTIME Year As Integer Month As Integer DayOfWeek As Integer Day As Integer Hour As Integer Minute As Integer Second As Integer Milliseconds As Integer End Type 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 Any, lpLastAccessTime As Any, lpLastWriteTime As Any) 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 'Purpose : To modify file creation, last accessed and last modified time for a file 'Inputs : sFileName The path and name of the file to alter. ' dFileDate The new file date. ' [bSetCreationTime] If True alters the file creation time. ' [bSetLastAccessedTime] If True alters the file last modified/accessed time. ' [bLastWriteTime] If True alters the file last write time. 'Outputs : Returns True if succeeded in altering the file time. 'Author : Andrew Baker 'Date : 03/09/2000 13:58 'Notes : 'Revisions : Function FileSetDate(ByVal sFileName As String, ByVal dFileDate As Date, Optional bSetCreationTime As Boolean = False, Optional bSetLastAccessedTime As Boolean = False, Optional bSetLastModified As Boolean = False) As Boolean Const GENERIC_WRITE = &H40000000, OPEN_EXISTING = 3 Const FILE_SHARE_READ = &H1, FILE_SHARE_WRITE = &H2 Dim lhwndFile As Long Dim tSystemTime As SYSTEMTIME Dim tLocalTime As FILETIME, tFileTime As FILETIME tSystemTime.Year = Year(dFileDate) tSystemTime.Month = Month(dFileDate) tSystemTime.Day = Day(dFileDate) tSystemTime.DayOfWeek = Weekday(dFileDate) - 1 tSystemTime.Hour = Hour(dFileDate) tSystemTime.Second = Second(dFileDate) tSystemTime.Milliseconds = 0 'Open the file to get the filehandle lhwndFile = CreateFile(sFileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0) If lhwndFile Then 'File opened 'Convert system time to local time SystemTimeToFileTime tSystemTime, tLocalTime 'Convert local time to GMT LocalFileTimeToFileTime tLocalTime, tFileTime '-------Change date/time property of the file FileSetDate = True If bSetCreationTime Then FileSetDate = FileSetDate And CBool(SetFileTime(lhwndFile, tFileTime, 0&, 0&)) End If If bSetLastAccessedTime Then FileSetDate = FileSetDate And CBool(SetFileTime(lhwndFile, 0&, tFileTime, 0&)) End If If bSetLastModified Then FileSetDate = FileSetDate And CBool(SetFileTime(lhwndFile, 0&, 0&, tFileTime)) End If 'Close the file handle Call CloseHandle(lhwndFile) End If End Function 'Demonstration routine Sub Test() 'Set the creation time FileSetDate "C:\Posters.html", Now, True 'Set the last accessed time FileSetDate "C:\Posters.html", Now, , True 'Set the last write time FileSetDate "C:\Posters.html", Now, , , True End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder