How to modify of property "creation date" for some document?

A

avkokin

Hello.
There is one document. I want to change of date of create it. Can I
modify the property "Creation Date" for this document? E.g, to some
predetermined date? How?
Thank you.
 
K

Karl E. Peterson

avkokin said:
There is one document. I want to change of date of create it. Can I
modify the property "Creation Date" for this document? E.g, to some
predetermined date? How?

You're talking about the "Date Created" as shown in Windows Explorer (or its
File-Properties dialogs)? If so, you'll want to use the SetFileTime API, which can
also modify the "Date Modified" and "Date Last Accessed" as well. Something like
this:

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 SetFileTime Lib "kernel32" (ByVal hFile As Long,
lpCreationTime As Any, lpLastAccessTime As Any, lpLastWriteTime As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As
Long

' API structures.
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

' Enumerated constants
Private Enum FileTimes
ftCreationTime = 0
ftLastAccessTime = 1
ftLastWriteTime = 2
End Enum

Private Function SetTime(FileName As String, NewTime As Double, WhichTime As
FileTimes) As Boolean
Dim ft As FILETIME
Dim hFile As Long

' Bail if no file exists.
If FileExists(FileName) = False Then Exit Function

' Convert passed time (presumably local) to UTC.
ft = DoubleToFileTime(NewTime, True)

' Get a handle on existing file so we can change times.
hFile = CreateFile(FileName, _
GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)

' If we were able to open file, change it's timestamp.
If hFile <> INVALID_HANDLE_VALUE Then
Select Case WhichTime
Case ftCreationTime
SetTime = SetFileTime(hFile, ft, ByVal 0&, ByVal 0&)
Case ftLastAccessTime
SetTime = SetFileTime(hFile, ByVal 0&, ft, ByVal 0&)
Case ftLastWriteTime
SetTime = SetFileTime(hFile, ByVal 0&, ByVal 0&, ft)
End Select
Call CloseHandle(hFile)
End If
End Function

Private Function DoubleToFileTime(ftDbl As Double, Optional Universalize As
Boolean = True) As FILETIME
Dim ft As FILETIME
Dim st As SYSTEMTIME

' Convert double to systemtime structure.
With st
.wYear = Year(ftDbl)
.wMonth = Month(ftDbl)
.wDay = Day(ftDbl)
.wDayOfWeek = WeekDay(ftDbl) - 1
.wHour = Hour(ftDbl)
.wMinute = Minute(ftDbl)
.wSecond = Second(ftDbl)
End With

' Convert systemtime to filetime structure.
Call SystemTimeToFileTime(st, ft)

' Convert local time to UTC time, if requested.
If Universalize Then
Call LocalFileTimeToFileTime(ft, DoubleToFileTime)
Else
DoubleToFileTime = ft
End If
End Function

Etc, etc, etc... All this and more, ready-to-run(!), at
http://vb.mvps.org/samples/FileInfo
 
A

avkokin

Hello Karl. Thank you very much. I think it will simpler :) I meaned
modification of the property the document "Creation date" which show
in dialogue PROPERTIES (menu File).
Sincerely, Anton Kokin
 
K

Karl E. Peterson

avkokin said:
Hello Karl. Thank you very much. I think it will simpler :) I meaned
modification of the property the document "Creation date" which show
in dialogue PROPERTIES (menu File).

Have you found that to be a different value?
 
A

akokin

Hello Karl.
Thank you. I found next solution:
Private Declare Function SystemTimeToFileTime& Lib _
"kernel32" (lpSystemTIME As SYSTEMTIME, _
lpFileTime As FILETIME)

Private Declare Function FileTimeToSystemTime& Lib _
"kernel32" (lpFileTime As FILETIME, lpSystemTIME _
As SYSTEMTIME)

' ÆÕÎËÃÉÉ ÒÁÂÏÔÙ Ó ÆÁÊÌÁÍÉ
Private Declare Function lopen& Lib "kernel32" _
Alias "_lopen" (ByVal lpFileName As String, _
ByVal wReadWhite As Long)

Private Declare Function lclose& Lib "kernel32" _
Alias "_lclose" (ByVal hFile As Long)

Private Declare Function SetFileTime& Lib "kernel32" _
(ByVal hFile As Long, lpCreationTime As FILETIME, _
lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME)

' ÆÕÎËÃÉÑ ÄÌÑ ÁÎÁÌÉÚÁ ÏÛÉÂÏË
Private Declare Function GetLastError& Lib "kernel32" ()

' ÄÌÑ ÈÒÁÎÅÎÉÑ ÄÁÔÙ ×Ï ×ÎÕÔÒÅÎÎÅÍ ÆÏÒÍÁÔÅ
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 Form_Load()
Dim SysTime As SYSTEMTIME, NowTime As FILETIME
Dim FileName$, handleF&, wReadWrite&, k&, k1&
' ÄÁÔÁ × ÓÉÓÔÅÍÎÏÍ ÆÏÒÍÁÔÅ
SysTime.wYear = 1997
SysTime.wMonth = 10
SysTime.wDay = 3
' ÐÒÅÏÂÒÁÚÏ×ÁÎÉÅ ÄÁÔÙ ×Ï ×ÎÕÔÒÅÎÎÉÊ Ä×ÏÉÞÎÙÊ ÆÏÒÍÁÔ
k& = SystemTimeToFileTime(SysTime, NowTime)
'
' ÉÍÑ ÆÁÊÌÁ - ÏÎ ÄÏÌÖÅÎ ÓÕÝÅÓÔ×Ï×ÁÔØ
FileName$ = "d:\d.txt"
' òÁÂÏÔÁ Ó ÆÁÊÌÁÍÉ ÔÏÌØËÏ ÓÒÅÄÓÔ×ÁÍÉ ÆÕÎËÃÉÊ API
' ÷îéíáîéå! äÌÑ ÉÚÍÅÎÅÎÉÑ ÁÔÒÉÂÕÔÏ× ÆÁÊÌÁ,
' ÏÎ ÄÏÌÖÅÎ ÂÙÔØ ÏÔËÒÙÔ × ÒÅÖÉÍÅ "ÒÁÚÒÅÛÅÎÉÑ
' ÚÁÐÉÓÉ", ÎÁÐÒÉÍÅÒ: ÒÅÖÉÍ "ÞÔÅÎÉÅ-ÚÁÐÉÓØ"
' Const OF_READWRITE& = 2
wReadWrite& = 2
' ïÔËÒÙÔÉÅ ÆÁÊÌÁ
handleF& = lopen&(FileName$, wReadWrite&)
' ÚÁÐÉÓØ ÎÏ×ÙÈ ÁÔÒÉÂÕÔÏ× ÄÁÔÙ
k& = SetFileTime&(handleF&, NowTime, NowTime, NowTime)
' ÂÙÌÁ ÌÉ ÏÛÉÂËÁ? íÏÖÎÏ ÐÒÏ×ÅÒÉÔØ
k1& = GetLastError ' ËÏÄ ÏÛÉÂËÉ
' ÚÁËÒÙÔÉÅ ÆÁÊÌÁ
Call lclose(handleF&)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top