• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

setfiletime problem?

Hi guys
I want some one give an example about useing this function I don't now how can I do that
for example I wrote :
Given:
VAR
  dt, newdt:  TDateTime;
...
dt := EncodeDate(1999, 12, 25) +  EncodeTime(20, 5, 30, 0);

I want to set ( dt : tdatetime) to be the file creation time and last time access and modefi

I want answer today

I am waiting

peruzzi
0
peruzzi
Asked:
peruzzi
  • 2
1 Solution
 
geobulCommented:
function SetFileDate(FileName: string; NewDate: TDateTime): Boolean;
var
 hFile: THandle;
 SystemTime: TSystemTime;
 FileTime, CreationTime, LastAccessTime, LastWriteTime: TFileTime;
begin
 result := false;
 hFile := CreateFile(PChar(FileName), GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0);

 if hFile <> INVALID_HANDLE_VALUE then
   try
     begin
       DateTimeToSystemTime(NewDate, SystemTime);
       SystemTimeToFileTime(SystemTime, FileTime);
       LocalFileTimeToFileTime(FileTime, CreationTime);
       LocalFileTimeToFileTime(FileTime, LastAccessTime);
       LocalFileTimeToFileTime(FileTime, LastWriteTime);
       SetFileTime(hFile, @CreationTime, @LastAccessTime, @LastWriteTime);
       result := true;
     end;
   finally
     CloseHandle(hFile);
   end;
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShortTimeFormat := 'hh:mm:ss';
  ShortDateFormat := 'dd/mm/yyyy';
  SetFileDate('c:\test.txt', StrToDateTime('01/01/2003 02:22'));
end;

Regards, Geo
0
 
geobulCommented:
In your case the usage will be even simplier:

VAR
  dt, newdt:  TDateTime;
...
dt := EncodeDate(1999, 12, 25) +  EncodeTime(20, 5, 30, 0);
SetFileDate('c:\test.txt', dt);

Regards, Geo
0
 
esoftbgCommented:
function  ChangeDateTimeOfFile(SD, ST, SFileName: string): Boolean;
var
  B:      Boolean;
  Age:    Integer;
  FHandle:Integer;
  DT:     TDateTime;
begin
  B := False;
  try
    FHandle := FileOpen(SFileName, fmOpenWrite);
    if (FHandle>0) then
    try
      DT := StrToDateTime(SD + ' ' + ST);
      Age := DateTimeToFileDate(DT);
      B := (FileSetDate(FHandle, Age)=0);
    finally
      FileClose(FHandle);
    end;
  finally
    Result := B;
  end;
end;

procedure TFormvvv.SpeedButton1Click(Sender: TObject);
begin
  ChangeDateTimeOfFile('04/27/2004', '1:00:00.000', 'C:\Temp.Exe');
end;
0
 
peruzziAuthor Commented:
Thanks for your help

peruzzi
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now