Set the last modified date and time of a file SysUtils unit
1  function FileSetDate ( FileHandle : Integer; FileAge : Integer ) : Integer;
2  function FileSetDate ( const FileName : string; FileAge : Integer ) : Integer;
The SetFileDate function sets the last modified date and time of a file.
Version 1 of this function takes a FileHandle parameter to indicate the file. This requires use of the FileOpen function, which is one of the low level file handling routines in Delphi.
Version 2 of this function takes a FileName parameter to define the file.
The file is searched for in the current directory.
The second parameter, FileAge is an Integer date/time representation that is used to change the last modified date/time of the file.
Use the DateTimeToFileDate function to convert a TDateTime value to this integer format.
Related commands
DateTimeToFileDate Convert a TDateTime value to a File date/time format
FileAge Get the last modified date/time of a file without opening it
FileDateToDateTime Converts a file date/time format to a TDateTime value
FileGetAttr Gets the attributes of a file
FileSetAttr Sets the attributes of a file
 Author links

 Download this web site as a Windows program.

Example code : Display, update, and redisplay the last modified date of a file
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
  SysUtils,   // Unit containing the FileSetDate command
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  fileName    : string;
  fileDate    : Integer;
  newDateTime : TDateTime;

  // Try to open the Unit1.DCU file for the current project
  fileName := 'Unit1.DCU';
  fileDate := FileAge(fileName);

  // Did we get the file age OK?
  if fileDate > -1 then
    ShowMessage(fileName+' last modified date = '+

    // Now change the last modified date
    newDateTime := StrToDateTime('01/01/2000 12:34:56');
    FileSetDate(fileName, DateTimeToFileDate(newDateTime));

  // Did we update the file last modified date OK?
  fileDate := FileAge(fileName);
  if fileDate > -1 then
    ShowMessage(fileName+' last modified date = '+
Hide full unit code
   Unit1.DCU last modified date = 30/10/2002 15:16:22
   Unit1.DCU last modified date = 01/01/2000 12:34:56
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page