[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to change a date on a file?

Posted on 1998-09-19
3
Medium Priority
?
200 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 200 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

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…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

656 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