1. Home
  2. Computing & Technology
  3. Delphi Programming

Get Directory Creation, Modified, Last Accessed and Last Write Date and Time

By , About.com Guide

While Delphi provides many (file and) directory related functions and procedures to help you operate on folders and file, quite a few tasks equire you to dive into the world of Windows API calls.

Date Time Info for a Directory

If your task is to track the "modified" date time value for a given directory, you simply need to take a look at the TSearchRec record returned by a call to the FindFirst function.

If you, on the other hand, need to get the creation time, for example, you need to dig deeply...

Here's an example function that will display all the important time values for a given directory: creation time, date and time the specified directory was last accessed and date and time the specified file or directory was last written to.

uses DateUtils;

procedure DirectoryDateTimeInfo(const path : string) ;
var
  sr : TSearchRec;
  modifiedTime : TDateTime;
  creationTimeSystem: TSystemTime;
  lastAccessTimeSystem: TSystemTime;
  lastWriteTimeSystem: TSystemTime;
  creationTime: TDateTime;
  lastAccessTime: TDateTime;
  lastWriteTime: TDateTime;
begin
  if sysUtils.FindFirst(path, faDirectory, sr) = 0 then
  try
    modifiedTime := FileDateToDateTime(sr.Time) ;

    FileTimeToSystemTime(sr.FindData.ftCreationTime, creationTimeSystem) ;
    with creationTimeSystem do
      creationTime := EncodeDateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds) ;

    FileTimeToSystemTime(sr.FindData.ftLastAccessTime, lastAccessTimeSystem) ;
    with lastAccessTimeSystem do
      lastAccessTime := EncodeDateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds) ;

    FileTimeToSystemTime(sr.FindData.ftLastWriteTime, lastWriteTimeSystem) ;
    with lastWriteTimeSystem do
      lastWriteTime := EncodeDateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds) ;

    ShowMessage(Format('Date Time Info for "%s"', [path])) ;
    ShowMessage(Format('Creation Time: "%s"', [DateTimeToStr(creationTime)])) ;
    ShowMessage(Format('Last Access Time: "%s"', [DateTimeToStr(lastAccessTime)])) ;
    ShowMessage(Format('Last Write Time: "%s"', [DateTimeToStr(lastWriteTime)])) ;
    ShowMessage(Format('Last Modified Time: "%s"', [DateTimeToStr(modifiedTime)])) ;
  finally
   FindClose(sr) ;
  end
  else
    ShowMessage(Format('Could not obtain info for "%s"', [path])) ;
end;

A quick info on the RTL + API functions used:

  • Find First / Find Close is used to fill in the TSearchRec ("sr" variable) record containing info on the file specified.
  • Last Modified time is stored in the "Time" field.
  • Delphi's FileDateToDateTime converts the system time stamp of a file to a TDateTime value.
  • FileTimeToSystemTime API converts a 64-bit file time to system time format (TSystemTime)
  • Delphi's EncodeDateTime returns a TDateTime represented by a specified year, month, day, hour, minute, second, and millisecond.
  • Finally, DateTimeToStr is used to convert the date and time value to a string.

Note: The DirectoryDateTimeInfo procedure will happily accept files!

Delphi tips navigator:
» Implementing "Contains Focus" for Delphi's Container Controls: TPanel, TGroupBox, ...
« Get Parent Folder / Directory

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2007 Tips
  7. Get Directory Creation, Modified, Last Accessed and Last Write Date and Time using Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.