Solved

setfiletime problem?

Posted on 2004-04-27
4
255 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

22 Experts available now in Live!

Get 1:1 Help Now