Solved

setfiletime problem?

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

808 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