Mirror

Touch a file with a specified date/time (Views: 100)


Problem/Question/Abstract:

How do I change the date & time of a file specified as a string?

Answer:

Often A file's time is set to represent a version number. For example the datetime may be January 27, 2000 1:03:00AM to represent version 1 patch 3.

This unit presents a procedure which takes two parameters, A file path/name specified as a string and a DateTime.
The specified file's date & time will be changed to match the DateTime specified.

unit Fileutil;

interface
uses System, SysUtils;

{To Change the Date/Time of a file}
procedure TouchFile(FileName: string; Date: TDateTime);

implementation

procedure TouchFile(FileName: string; Date: TDateTime);
var
  TheFile: file;
begin
  AssignFile(TheFile, FileName);
  Reset(TheFile);
  FileSetDate(TFileRec(TheFile).Handle,
    DateTimeToFileDate(Date));
  Close(TheFile);
end;

end.

<< Back to main page