hi people.
how can I change the "Data Createted" and "Last Modify".
I need that for a file "Name.txt" or "Name.doc"
thanks for any sugestion.
You can use the SetFileTime API function to do that. See the example code below. The function VBSetFileTimes accepts a filename and three optional time arguments which dictate the creation time, last access and modified time.
These three time arguments are optional. If you do not specify one or more of these time arguments then these time attributes remain unchanged.
___
[tt]
Option Explicit
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) 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 LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
Private Const OPEN_EXISTING = 3
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 Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Sub Form_Load()
'modify all times
VBSetFileTimes "C:\Name.txt", #1/4/2002 5:23:36 AM#, #6/5/2003#, #1/5/2003 3:56:33 PM#
'modify only the last access time
VBSetFileTimes "C:\Name.txt", , #6/6/2003#
Unload Me
End Sub
Sub VBSetFileTimes(FileName As String, Optional CreationTime, Optional LastAccessTime, Optional LastModifiedTime)
Dim hFile As Long, ft1 As FILETIME, ft2 As FILETIME, ft3 As FILETIME
hFile = CreateFile(FileName, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0&, OPEN_EXISTING, 0, 0)
If hFile Then
GetFileTime hFile, ft1, ft2, ft3
If Not IsMissing(CreationTime) Then VBDateToFileTime CreationTime, ft1
If Not IsMissing(LastAccessTime) Then VBDateToFileTime LastAccessTime, ft2
If Not IsMissing(LastModifiedTime) Then VBDateToFileTime LastModifiedTime, ft3
SetFileTime hFile, ft1, ft2, ft3
CloseHandle hFile
End If
End Sub
Private Sub VBDateToFileTime(ByVal Dt As Date, ft As FILETIME)
Dim st As SYSTEMTIME
st.wYear = Year(Dt)
st.wMonth = Month(Dt)
st.wDayOfWeek = Weekday(Dt)
st.wDay = Day(Dt)
st.wHour = Hour(Dt)
st.wMinute = Minute(Dt)
st.wSecond = Second(Dt)
SystemTimeToFileTime st, ft
LocalFileTimeToFileTime ft, ft
End Sub
[/tt]
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.