Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Windows - Cannot set file date top a date prior to 1980 ...

1 Posts
1 Users
0 Reactions
1,514 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2796
Topic starter  

So I ran into this little issue ... (thanks to user Granny646)
Since Windows 8, it seems that Windows doesn't like file dates prior to 1980.
This seems a mixed bag of certain functions (for example FileSetDate) not properly supporting dates like that and the Windows File Explorer not displaying it either.

Again: Windows File Explorer will not show a date when file date is set to a date to prior 1980.

Example:

However: this is not a filesystem limitation (tested with NTFS, not and issue under macOS and Linux either).

So how do we set the file date with a date like that in Lazarus Pascal?
I found a little function for that (minor modification from this one by howardpc):

uses ... FileUtil, Windows ...

...

function SetFileDate(const aFilename: string; aDate: TDateTime): boolean;
var
  fileHandle : THandle;
  fileTime   : TFILETIME;
  LFileTime  : TFILETIME;
  LSysTime   : TSystemTime;
begin
  Result:=False;

  if not(DirectoryExists(aFilename) or FileExists(aFilename)) then exit;

  try
    DecodeDate(aDate, LSysTime.Year, LSysTime.Month, LSysTime.Day);
    DecodeTime(aDate, LSysTime.Hour, LSysTime.Minute, LSysTime.Second, LSysTime.Millisecond);

    if SystemTimeToFileTime(LSysTime, LFileTime) then
      begin
        if LocalFileTimeToFileTime(LFileTime, fileTime) then
          begin
            fileHandle:=FileOpenUTF8(aFilename, fmOpenReadWrite or fmShareExclusive);
            if SetFileTime(fileHandle, fileTime, fileTime, fileTime) then
              Result:=True;
          end;
      end;
  finally
    FileClose(fileHandle);
  end;
end;

 

 Hope this is useful to someone ....


   
ReplyQuote
Share: