Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2006 Mar : Setting custom last write date for a file...
| Subject: | Setting custom last write date for a file... |
| Posted by: | "Adrien Reboisson" (adrien-reboissonatastasedotc..@csm142.com) |
| Date: | 30 Mar 2006 05:53:34 |
Hi,
I'm using the following code, extracted from the JCL library, in
order to set a custom last write date for a given file :
function SetFileTimesHelper(const FileName: string; const DateTime: TDateTime; Times: TFileTimes): Boolean;
var
Handle: THandle;
FileTime: TFileTime;
SystemTime: TSystemTime;
begin
Result := False;
Handle := CreateFile(PChar(FileName), GENERIC_WRITE, FILE_SHARE_READ, nil,
OPEN_EXISTING, 0, 0);
if Handle <> INVALID_HANDLE_VALUE then
try
SysUtils.DateTimeToSystemTime(DateTime, SystemTime);
if Windows.SystemTimeToFileTime(SystemTime, FileTime) then
begin
case Times of
ftLastAccess:
Result := SetFileTime(Handle, nil, @FileTime, nil);
ftLastWrite:
Result := SetFileTime(Handle, nil, nil, @FileTime);
ftCreation:
Result := SetFileTime(Handle, @FileTime, nil, nil);
end;
end;
finally
CloseHandle(Handle);
end;
end;
Most of the time, it works. But for some files, it bugs ! I don't
know why... For instance, this following code should change the
file date to "6 Mars 2002 / 4h 38m 8s" for the file
"C:\Dev-Cpp\Examples\WinTest\test.c" :
procedure TForm1.Button1Click(Sender: TObject);
var
D: TDateTime;
begin
D := EncodeDateTime(2002, 03, 06, 04, 34, 08, 0);
// = 6 Mars 2002, 4h 38m 8s
if SetFileLastWrite('C:\Dev-Cpp\Examples\WinTest\test.c', D) then
MessageDlg('OK !', mtInformation, [mbOK], 0); //displayed
end;
(SetFileLastWrite calls internally SetFileTimesHelper).
The "OK" dialog is displayed, showing that no error was
encountered. Nevertheless, the file date is always set to
"mercredi 6 mars 2002, 06:34:08". This 2 hours offset is driving
me nuts. Why ?? The code I wrote has to be included into a kind
of backup engine, and setting a wrong date to a file make the
backup engine thinking that the file has changed while it's
false.
And I don't see where is the bug in the JCL function.
Any hints greatly appreciated ;-)
Best Regards,
A.R.