Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-12-27
7
Medium Priority
?
655 Views
Last Modified: 2013-11-23
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!
0
Comment
Question by:andreamatt
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26127979
You can use MoveFile(oldname,newName) to rename it. The Creation date will be not changed.
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26127985
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
 
LVL 1

Author Comment

by:andreamatt
ID: 26128419
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 1820 total points
ID: 26128532
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
 
LVL 1

Author Comment

by:andreamatt
ID: 26131595
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
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26131764
@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
 
LVL 1

Author Comment

by:andreamatt
ID: 26136214
OK, Many thanks!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question