Delphi 6: How can I change the created filedate (= file creation date)

I searched HOURS now on Google (and here).

And I can not find a solution.

I want to CHANGE " Created by Filetime" (= file creation time) in DELPHI 6 .

Not "Modified file time" (which requires a simple call to "FileSetDate ()") and not "Last access time to files".

How to do it?

Picture of what I mean ...

+8
date file delphi delphi-6
source share
2 answers

Call the SetFileTime function of the Windows API. Pass nil for lpLastAccessTime and lpLastWriteTime if you want to change the creation time.

You will need to get the file descriptor by calling CreateFile or one of the Delphi shells, so this is not the most convenient API to use.

Make life easier for yourself by wrapping the API call in a helper function that gets the file name and TDateTime . This function should manage low-level information about getting and closing a file descriptor and convert TDateTime to FILETIME .

I would do it like this:

 const FILE_WRITE_ATTRIBUTES = $0100; procedure SetFileCreationTime(const FileName: string; const DateTime: TDateTime); var Handle: THandle; SystemTime: TSystemTime; FileTime: TFileTime; begin Handle := CreateFile(PChar(FileName), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if Handle=INVALID_HANDLE_VALUE then RaiseLastOSError; try DateTimeToSystemTime(DateTime, SystemTime); if not SystemTimeToFileTime(SystemTime, FileTime) then RaiseLastOSError; if not SetFileTime(Handle, @FileTime, nil, nil) then RaiseLastOSError; finally CloseHandle(Handle); end; end; 

I had to add the FILE_WRITE_ATTRIBUTES declaration because it is not in the Windows Delphi 6 block.

+6
source share

Based on FileSetDate you can write a similar procedure:

 function FileSetCreatedDate(Handle: Integer; Age: Integer): Integer; var LocalFileTime, FileTime: TFileTime; begin Result := 0; if DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime) and LocalFileTimeToFileTime(LocalFileTime, FileTime) and SetFileTime(Handle, @FileTime, nil, nil) then Exit; Result := GetLastError; end; 
+7
source share

All Articles