|
其實 Windows 檔案總管更改時問是很簡單,這是API例子
Option Explicit
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 Const sPromptDate As String = "Enter date (DD-MM-YYYY format)"
Private Const sTitleDate As String = "Date Entry"
Private Const sPromptTime As String = "Enter time [Optional](HH:MM:SS format)"
Private Const sTitleTime As String = "Time Entry"
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
'*-----------------------------------------------------------------------
'* This routine sets the date and option ally the time of the
'* specified file.
'*
'* Inputs:
'* sFileName; String containing the full path to the file
'* sDate; String containing the date (DD-MM-YYYY format)
'* sTime; (Optional) string containing the time in HH:MM:SS format
'* Thanks to the KPDTeam
'* Mods by Ivan F Moala 12/11/2002
'*-----------------------------------------------------------------------
Private Function SetFileDate(ByVal sFileName As String, ByVal sDate As String, _
Optional sTime As String = "00:00:00") As Boolean
Dim dDate As Date
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim lFileHandle As Long
Dim RetVal As Long
'// Make sure we format it properly before converting
dDate = format(sDate & " " & sTime, "DD-MM-YYYY HH:MM:SS")
With udtSystemTime
.wYear = Year(dDate)
.wMonth = Month(dDate)
.wDay = Day(dDate)
.wDayOfWeek = Weekday(dDate) - 1
.wHour = Hour(dDate)
.wMinute = Minute(dDate)
.wSecond = Second(dDate)
.wMilliseconds = 0
End With
'// Convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
'// Convert local time to GMT (UTC Universal Time Coordinates)
'// I prefer to call it GMT Greenwich Mean Time
LocalFileTimeToFileTime udtLocalTime, udtFileTime
'// Open the file for Read/Write to get the filehandle
lFileHandle = CreateFile( _
sFileName, _
GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
ByVal 0&, _
OPEN_EXISTING, _
0, _
0)
'// Do we have a handle?
If lFileHandle = 0 Then SetFileDate = False: GoTo ExitProperly
'// Change date/time property of the file
RetVal = SetFileTime(lFileHandle, udtFileTime, udtFileTime, udtFileTime)
'// Was it changed?
If RetVal = 0 Then SetFileDate = False: GoTo ExitProperly
SetFileDate = True
ExitProperly:
'// Close the file handle
CloseHandle lFileHandle
End Function
Sub Tester()
Dim OK As Boolean
Dim strMyFileName As String
Dim strMyDate As String
Dim strMyTime As String
Dim objCal As Object
strMyFileName = Application.GetOpenFilename("Any File (*.*), *.*")
If strMyFileName = "Fasle" Then Exit Sub
'format = DD-MM-YYYY
DateAgain:
strMyDate = Application.InputBox(sPromptDate, sTitleDate, Type:=2)
If strMyDate = "False" Then Exit Sub
If Not VerifydateTime(strMyDate, True) Then GoTo DateAgain
'format = HH:MM:SS
TimeAgain:
strMyTime = Application.InputBox(sPromptTime, sTitleTime, Type:=2)
If strMyTime = "False" Then Exit Sub
If Len(strMyTime) = 0 Then GoTo Process
If Not VerifydateTime(strMyTime, False) Then GoTo TimeAgain
Process:
OK = SetFileDate(strMyFileName, strMyDate, strMyTime)
MsgBox strMyFileName & " Date changed:=" & OK
End Sub
Function VerifydateTime(sDate As String, DateOrTime As Boolean) As Boolean
Select Case DateOrTime
Case True
If Len(sDate) <> 10 Then VerifydateTime = False: Exit Function
On Error Resume Next
If Not IsDate(CVDate(sDate)) Then
'//
VerifydateTime = False
Exit Function
End If
If MsgBox(CVDate(sDate) & " OK", vbYesNo) = vbNo Then
VerifydateTime = False
Exit Function
Else
VerifydateTime = True
End If
Case Else
If Len(sDate) <> 8 Then VerifydateTime = False: Exit Function
On Error Resume Next
If MsgBox(Timevalue(sDate) & " OK", vbYesNo) = vbNo Then
VerifydateTime = False
Exit Function
Else
VerifydateTime = True
End If
End Select
End Function |
|