|
Изменить дату последней модификации файла |
|
|
Расположите на форме элемент CommandButton. Замените путь к файлу "C:\1.txt" на путь к вашему файлу. В Проводнике нажмите правой клавишей мыши на вашем файле и посмотрите дату, когда файл был "Изменен:". Запустие нижеприведенный код на выполнение и снова посмотрите на дату изменения файла. Option Explicit
Private Type FILETIME
dwLowDate As Long
dwHighDate 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
wMillisecs As Integer
End Type
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const GENERIC_WRITE = &H40000000
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 LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime
As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, ByVal
MullP As Long, ByVal NullP2 As Long, 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
Public Function SetFileDateTime(ByVal FileName As String, ByVal TheDate As String) As
Boolean
'параметры: TheDate - дата последней модификации
файла
'FileName - имя файла
'функция возвращает: True в случае успеха, False - в
обратном случае
If Dir(FileName) = "" Then Exit Function
If Not IsDate(TheDate) Then Exit Function
Dim lFileHnd As Long
Dim lRet As Long
Dim typFileTime As FILETIME
Dim typLocalTime As FILETIME
Dim typSystemTime As SYSTEMTIME
With typSystemTime
.wYear = Year(TheDate)
.wMonth = Month(TheDate)
.wDay = Day(TheDate)
.wDayOfWeek = Weekday(TheDate) - 1
.wHour = Hour(TheDate)
.wMinute = Minute(TheDate)
.wSecond = Second(TheDate)
End With
lRet = SystemTimeToFileTime(typSystemTime, typLocalTime)
lRet = LocalFileTimeToFileTime(typLocalTime, typFileTime)
lFileHnd = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal
0&, OPEN_EXISTING, 0, 0)
lRet = SetFileTime(lFileHnd, ByVal 0&, ByVal 0&, typFileTime)
CloseHandle lFileHnd
SetFileDateTime = lRet > 0
End Function
Private Sub Command1_Click()
Dim a As Boolean
a = SetFileDateTime("c:\1.txt", Now)
'или: Call SetFileDateTime("c:\1.txt", "21/01/1998")
End Sub
|
|
|
|
|
|
|