Solved

How to change a date on a file?

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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

18 Experts available now in Live!

Get 1:1 Help Now