Solved

How to change a date on a file?

Posted on 1998-09-19
3
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 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