Solved

setfiletime problem?

Posted on 2004-04-27
4
260 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
[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
  • 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

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!

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

724 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