whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

88 Visitors Online


 
...get the last access from a file?
Autor: Simon Grossenbacher
Homepage: http://www.swissdelphicenter.ch
[ Print tip ]  

Tip Rating (17):  
     


{1.}

function GetFileLastAccessTime(sFileName: string): TDateTime;
var
  
ffd: TWin32FindData;
  dft: DWORD;
  lft: TFileTime;
  h:   THandle;
begin
  
//
  // get file information
  
h := Windows.FindFirstFile(PChar(sFileName), ffd);
  if (INVALID_HANDLE_VALUE <> h) then
  begin
    
//
    // we're looking for just one file,
    // so close our "find"
    
Windows.FindClose(h);
    //
    // convert the FILETIME to
    // local FILETIME
    
FileTimeToLocalFileTime(ffd.ftLastAccessTime, lft);
    //
    // convert FILETIME to
    // DOS time
    
FileTimeToDosDateTime(lft, LongRec(dft).Hi, LongRec(dft).Lo);
    //
    // finally, convert DOS time to
    // TDateTime for use in Delphi's
    // native date/time functions
    
Result := FileDateToDateTime(dft);
  end;
end;


{********************************************************************}

{2.}

function GetFileTimes(const FileName: stringvar Created: TDateTime;
var Accessed: TDateTime; var Modified: TDateTime): Boolean;
var
  
h: THandle;
  Info1, Info2, Info3: TFileTime;
  SysTimeStruct: SYSTEMTIME;
  TimeZoneInfo: TTimeZoneInformation;
  Bias: Double;
begin
  
Result := False;
  Bias   := 0;
  h      := FileOpen(FileName, fmOpenRead or fmShareDenyNone);
  if h > 0 then 
  begin
    try
      if 
GetTimeZoneInformation(TimeZoneInfo) <> $FFFFFFFF then
        
Bias := TimeZoneInfo.Bias / 1440; // 60x24
      
GetFileTime(h, @Info1, @Info2, @Info3);
      if FileTimeToSystemTime(Info1, SysTimeStruct) then
        
Created := SystemTimeToDateTime(SysTimeStruct) - Bias;
      if FileTimeToSystemTime(Info2, SysTimeStruct) then
        
Accessed := SystemTimeToDateTime(SysTimeStruct) - Bias;
      if FileTimeToSystemTime(Info3, SysTimeStruct) then
        
Modified := SystemTimeToDateTime(SysTimeStruct) - Bias;
      Result := True;
    finally
      
FileClose(h);
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  
Date1, Date2, Date3: TDateTime;
begin
  if 
GetFileTimes(Edit1.Text, Date1, Date2, Date3) then 
  begin
    
ShowMessage('Created: ' + DateTimeToStr(Date1));
    ShowMessage('Last Accessed: ' + DateTimeToStr(Date2));
    ShowMessage('Last Modified: ' + DateTimeToStr(Date3));
  end;
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners