...set a file's date ?

Author: Simon Grossenbacher
Homepage: http://www.swissdelphicenter.ch

Category: Files

function SetFileDateTime(FileName: string; NewDateTime: TDateTime): Boolean;
var
  
FileHandle: Integer;
  FileTime: TFileTime;
  LFT: TFileTime;
  LST: TSystemTime;
begin
  
Result := False;
  try
    
DecodeDate(NewDateTime, LST.wYear, LST.wMonth, LST.wDay);
    DecodeTime(NewDateTime, LST.wHour, LST.wMinute, LST.wSecond, LST.wMilliSeconds);
    if SystemTimeToFileTime(LST, LFT) then
    begin
      if 
LocalFileTimeToFileTime(LFT, FileTime) then
      begin
        
FileHandle := FileOpen(FileName, fmOpenReadWrite or
          
fmShareExclusive);
        if SetFileTime(FileHandle, nilnil, @FileTime) then
          
Result := True;
      end;
    end;
  finally
    
FileClose(FileHandle);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if 
OpenDialog1.Execute then
    if 
SetFileDateTime(OpenDialog1.FileName, now) then
      
ShowMessage('Date set to now !');
end;

 

printed from
www.swissdelphicenter.ch
developers knowledge base