Solved

setfiletime problem?

Posted on 2004-04-27
4
256 Views
Last Modified: 2010-04-05
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
Comment
Question by:peruzzi
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 100 total points
ID: 10927643
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
 
LVL 17

Expert Comment

by:geobul
ID: 10927668
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 10928664
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
 

Author Comment

by:peruzzi
ID: 10941840
Thanks for your help

peruzzi
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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
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 …

920 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

16 Experts available now in Live!

Get 1:1 Help Now