Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Changing Creation and Last accessed dat/time for filesFormat this article printer-friendly!Bookmark function is only available for registered users!
Changing file date/times properly
Product:
Delphi 3.x (or higher)
Category:
Win API
Skill Level:
Scoring:
Last Update:
10/03/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet FileSetDate SetFileTime DosDateTimeToFileTime
Times Scored:
4
Visits:
4628
Uploader: david bolton
Company:
Reference: N/A
 
Question/Problem/Abstract:
The normal sysutils function FileSetdate only alters the Last Written Time and does NOT set the date of creation or the date of last access. All Win 32 files from Windows 95 store these. How does one alter these?
Answer:



You can view the File creation and access times from Windows Explorer on W2k (and possibly XP) by right clicking the column headers in the details and adding the columns. Programmatically, the TSearchRec (used in FindFirst & FindNext) returns a  record of type Twin32FindData and this contains these values as well. This record from windows.pas shows these extra fields:

  _WIN32_FIND_DATAA = record
    dwFileAttributes: DWORD;
    ftCreationTime: TFileTime;          <--------
    ftLastAccessTime: TFileTime;        <--------
    ftLastWriteTime: TFileTime;
    nFileSizeHigh: DWORD;
    nFileSizeLow: DWORD;
    dwReserved0: DWORD;
    dwReserved1: DWORD;
    cFileName: array[0..MAX_PATH - 1] of AnsiChar;
    cAlternateFileName: array[0..13] of AnsiChar;



Also in windows.pas in FileSetDate, and by modifying the two nil parameters in the SetFileTime call to @FileTime you can set creation and last access to the same value when setting last written time. Here is my version of that.


function MyFileSetDate(Handle: Integer; Age: Integer): Integer;
var
  LocalFileTime, FileTime: TFileTime;
begin
  Result := 0;
  if DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime) and
    LocalFileTimeToFileTime(LocalFileTime, FileTime) and
    SetFileTime(Handle, @FileTime, @FileTime, @FileTime) then Exit;
  Result := GetLastError;
end;


The example below processes a path recursively and sets the file dates etc to that specified.


procedure TfrmTouch.ProcessPath(Filepath : string);
var sr : tSearchrec;
ErrorValue : integer;
Filename : string;

function MyFileSetDate(Handle: Integer; Age: Integer): Integer;
var
  LocalFileTime, FileTime: TFileTime;
begin
  Result := 0;
  if DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime) and
    LocalFileTimeToFileTime(LocalFileTime, FileTime) and
    SetFileTime(Handle, @FileTime, @FileTime, @FileTime) then Exit;
  Result := GetLastError;
end;

procedure ProcessFile;      
var
fh : integer;
fd : double;
dfd : integer;

begin  // Date is 20th May 1998 at 2:14:31 secs (AM)
  fd := EncodeDate(1998,5,20)+encodetime(2,14,31,0);
  dfd := DateTimeToFileDate(fd);
  fh := FileOpen(Filepath+filename,  fmOpenWrite or fmShareDenyNone);
  fd := Myfilesetdate(fh,dfd);
  fileclose(fh);
end;

begin
  if FilePath[length(FilePath)]<>'\' then
    Filepath := FilePath +'\';
  ErrorValue := Findfirst(Filepath+'*.*',faAnyfile,Sr);
  while ErrorValue =0 do
    begin
      Filename := Sr.FindData.cFileName;
      if (Sr.Attr and faDirectory = faDirectory) then
        begin
          if (Sr.Name[1]<>'.') and RecurseTree then
            ProcessPath(Filepath+Filename);
        end
      else
        ProcessFile;
      ErrorValue := FindNext(Sr);
    end;
end;

procedure TfrmTouch.BtnStartClick(Sender: TObject);
var path : string;
begin
  path := '';
  Screen.Cursor := crHourglass;
  Attrval := 0;
  Count := 0;
  Total := 0;
  RecurseTree := true;
  ProcessPath(Path);
  Screen.Cursor := crDefault;
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Can we change creation and Last accessed dat/time for folder?
    anonymus (Oct 5 2002 10:02PM)

Can we change creation and Last accessed dat/time for folder on win95 or win98?

I try some codes. I succesfull change folder date winnt, 2000 and xp but i can not change win95 or win98.


function GetFolderDate(Folder: string): TDateTime;
var
  Rec: TSearchRec;
  Found: Integer;
  Date: TDateTime;
begin
  if Folder[Length(folder)] = '\' then
    Delete(Folder, Length(folder), 1);
  Result := 0;
  Found  := FindFirst(Folder, faDirectory, Rec);
  try
    if Found = 0 then
    begin
      Date   := FileDateToDateTime(Rec.Time);
      Result := Date;
    end;
  finally
    FindClose(Rec);
  end;
end;

function NT_SetDateTime(FileName: string; dtCreation, dtLastAccessTime, dtLastWriteTime: TDateTime): Boolean;
var
  hDir: THandle;
  ftCreation: TFiletime;
  ftLastAccessTime: TFiletime;
  ftLastWriteTime: TFiletime;

  function DTtoFT(dt: TDateTime): TFiletime;
  var
    dwft: DWORD;
    ft: TFiletime;
  begin
    dwft := DateTimeToFileDate(dt);
    DosDateTimeToFileTime(LongRec(dwft).Hi, LongRec(dwft).Lo, ft);
    LocalFileTimeToFileTime(ft, Result);
  end;

begin
  hDir := CreateFile(PChar(FileName),
                     GENERIC_READ or GENERIC_WRITE,
                     0,
                     nil,
                     OPEN_EXISTING,
                     FILE_FLAG_BACKUP_SEMANTICS,
                     0);
  if hDir <> INVALID_HANDLE_VALUE then
  begin
    try
      ftCreation       := DTtoFT(dtCreation);
      ftLastAccessTime := DTtoFT(dtLastAccessTime);
      ftLastWriteTime  := DTtoFT(dtLastWriteTime);
      Result := SetFileTime(hDir, @ftCreation, @ftLastAccessTime, @ftLastWriteTime);
    finally
      CloseHandle(hDir);
    end;
  end
  else
    Result := False;
end;

Respond

RE: Can we change creation and Last accessed dat/time for folder?
david bolton (Oct 7 2002 10:08PM)

Its been a few years since I last used Win 95/98 but the Win32 help file says that the functions I used in my code should work on 95/98. However folders, I'm not sure.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Kleiner
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)