Advertisement
5_2007-2008 Files/ File Controls/ Input/ Output #175971

Change FileTime

This is a compact code that changes the date of an at the moment hard coded file to the time at the moment.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Option Explicit
Private Const GENERIC_WRITE As Long = &H40000000
Private Const GENERIC_READ As Long = &H80000000
Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80
Private Const CREATE_ALWAYS As Long = 2
Private Const OPEN_ALWAYS As Long = 4
Private Const INVALID_HANDLE_VALUE As Long = -1
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As Long, _
 ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, _
 ByVal lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, _
 lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, _
 lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
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 FileTimeToSystemTime Lib "kernel32" _
 (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
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 Sub Command1_Click()
Dim fHandle As Long
Dim FILE_NAME As String
FILE_NAME = "c:\test.txt" 'File with the dates to change
Dim FTime As FILETIME
fHandle = CreateFile(FILE_NAME, GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
If fHandle <> INVALID_HANDLE_VALUE Then
 FTime = GetSysTimeAsFILETIME
 SetFileTime fHandle, FTime, FTime, FTime
 CloseHandle fHandle
End If
End Sub
Private Function GetSysTimeAsFILETIME() As FILETIME
Dim SysTime As SYSTEMTIME
Dim FTime As FILETIME
Dim erg As Long
GetSystemTime SysTime
erg = SystemTimeToFileTime(SysTime, FTime)
GetSysTimeAsFILETIME = FTime
End Function
Original Comments (3)
Recovered from Wayback Machine