Rename directory without change the date-time creation of the directory

Hi,

is there a way in Delphi 7 to rename a dir without change the DateTime creation of the directory?
Or, another way, there is a sytem to modify the DateTime of a dir?
I prefer the first way...

Thanks!
LVL 1
andreamattAsked:
Who is Participating?
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
I guess you mean the last access datetime or the last access writing datetime, because using movefile don't change the creation datetime (as asked by you in this question ;-))
BTW you now need a function that can change both creation, last access and last modified datetime.

function TForm1.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;

Use it as follow
date1, date2, date3: TdateTime;
SetDateTime('c:\myfolder',date1,date2,date3);

Open in new window

0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
You can use MoveFile(oldname,newName) to rename it. The Creation date will be not changed.
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
In addition here you can find many tips about files/folder operations in delphi
http://www.greatis.com/delphicb/tips/lib/filesdirectories~title.html
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
andreamattAuthor Commented:
Thanks, but I already use the MoveFile. And I saw that did not work. After what you've written I've redone some tests and I discovered that in fact MoveFile works well!

The problem is then another.

This is an example.
I have a dir:
C:\ mydir
dated 01/01/2009

If I copy in C:\ mydir a file, the date of the directory changes in the current date!

This occurs even when using Windows Explorer.
There is a way to avoid this?
There is the possibility of changing the date of a dir?

I have see a function in the JVCL components but this don't work!

I have increased the point value of the solution.
0
 
andreamattAuthor Commented:
Hi,
good is work!
I have just a final problem... is there a way to set only the dtLastWriteTime in your function?

Because I use this function:

http://delphi.about.com/od/delphitips2007/qt/directory_dates.htm

to have the 3 DateTime (dtCreation, dtLastAccessTime, dtLastWriteTime) from the directory but with the dtCreation i have a different result:I always have an hour less than the real one!

Thanks!
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
@I have just a final problem... is there a way to set only the dtLastWriteTime in your function?
Yes, as follows
function SetDateTime(FileName: string; dtLastWriteTime: TDateTime): Boolean;
  var
    hDir: THandle;
    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
        ftLastWriteTime := DTtoFT(dtLastWriteTime);
        Result := SetFileTime(hDir, nil, nil, @ftLastWriteTime);
      finally
        CloseHandle(hDir);
      end;
    end
    else
      Result := False;
  end;


  SetDateTime('c:\myfolder', now-1000);

Open in new window

0
 
andreamattAuthor Commented:
OK, Many thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.