Solved

How to change a date on a file?

Posted on 1998-09-19
3
180 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IExtractImage Delphi 14 207
FMX StringGrid1->Canvas->FillRect Problem 3 128
When I am typing into a TEdit last word repeats 7 117
Making delphi communicate with a c# service 16 90
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now