Solved

How to change a date on a file?

Posted on 1998-09-19
3
188 Views
Last Modified: 2010-04-03
Can someone tell me how to change a date on a file? For example if a file called test.txt shows a 08-08-98 date, how can I make it 08-05-98?

              Thanks
0
Comment
Question by:jexd99
3 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
ID: 1340277
Hi

Below is a routine that you can use to change the file date & time.  There are 3 dates & times for a file:  (right-click on any file & choose properties) Created, Accessed and Modified.

With this routine you can specify which date or time you wish to change, although you would usually change the modified (tfAccess) date/time only.

type
  TTouchFile = (tfCreate, tfAccess, tfWrite);
  TTouchFiles = set of TTouchFile;

// To change file date only: Set NewTime to -1.
// To change file time only: Set NewDate to -1.
function TouchFile(const AFilename: string; NewDate, NewTime: TDateTime; Touch: TTouchFile): Boolean;
var
  OldDateTime, OldDate, OldTime, NewDateTime: TDateTime;
  ftCreate, ftAccess, ftWrite: TFileTime;
  hFile: TFileStream;
begin
  Result := True;
  try
    hFile := TFileStream.Create(AFilename, fmOpenReadWrite or fmShareDenyNone);
    try
      OldDateTime := 0;
      GetFileTime(hFile.Handle, @ftCreate, @ftAccess, @ftWrite);
      case Touch of
        tfCreate: OldDateTime := FileTimeToDateTime(ftCreate);
        tfAccess: OldDateTime := FileTimeToDateTime(ftAccess);
        tfWrite: OldDateTime := FileTimeToDateTime(ftWrite);
      end;
      OldDate := StrToDate(DateToStr(OldDateTime));
      OldTime := StrToTime(TimeToStr(OldDateTime));
      if (NewDate = -1) then
        NewDate := OldDate;
      if (NewTime = -1) then
        NewTime := OldTime;
      NewDateTime := NewDate + NewTime;
      case Touch of
        tfCreate: ftCreate := DateTimeToFileTime(NewDateTime);
        tfAccess : ftAccess := DateTimeToFileTime(NewDateTime);
        tfWrite: ftWrite := DateTimeToFileTime(NewDateTime);
      end;
      SetFileTime(hFile.Handle, @ftCreate, @ftAccess, @ftWrite);
    finally
      hFile.Free;
    end;
  except
    Result := False;
  end;
end;

Note that the Windows API functions I use are based on GMT (Greenwich Mean Time??) so you might need to make adjustments if you live in a different time zone (as I do).

Regards,
JB

0
 
LVL 7

Expert Comment

by:Motaz
ID: 1340278
This is a very simple code :

procedure TForm1.Button1Click(Sender: TObject);
var
    Age,Handle:integer;
begin
 Age:=FileDateTimeToFileDate(StrToDateTime('08-05-98'));
  Handle:=FileOpen('Test.Txt',fmOpenWrite);
  FileSetDate(Handle,Age);
  FileClose(Handle);
end;
// Hope it work..

Motaz form Sudan.
motaz1@yahoo.com
0
 

Author Comment

by:jexd99
ID: 1340279
Thanks for the info.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…

749 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