Solved

Date and Time

Posted on 2001-06-25
8
190 Views
Last Modified: 2010-04-06
I want to set the last access/modify/create details of a file to a specified date - let's say 18th December 1998 - by clicking on a button.

I found this information in the Win32 help file, but i am unsure how to use this in delphi.

----// start win32 help file //----

The SetFileTime function sets the date and time that a file was created, last accessed, or last modified.

BOOL SetFileTime(

    HANDLE hFile,     // identifies the file
    CONST FILETIME *lpCreationTime,     // time the file was created
    CONST FILETIME *lpLastAccessTime,     // time the file was last accessed
    CONST FILETIME *lpLastWriteTime      // time the file was last written
   );    
 

Parameters

hFile

Identifies the file for which to set the dates and times. The file handle must have been created with GENERIC_WRITE access to the file.

lpCreationTime

Points to a FILETIME structure that contains the date and time the file was created. This parameter can be NULL if the application does not need to set this information.

lpLastAccessTime

Points to a FILETIME structure that contains the date and time the file was last accessed. The last access time includes the last time the file was written to, read from, or (in the case of executable files) run. This parameter can be NULL if the application does not need to set this information.

lpLastWriteTime

Points to a FILETIME structure that contains the date and time the file was last written to. This parameter can be NULL if the application does not want to set this information.

---// end win32 help file //----


specifically how do i setup the FILETIME structure... for that matter if i know the date i want to set am i required to do that???

thanks for your help.


0
Comment
Question by:frangers99
  • 3
  • 3
  • 2
8 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6224033
Try this:


function FileSetCreationDate(Handle: Integer; Age: Integer): Integer;
var
 LocalFileTime, FileTime: TFileTime;
begin
 Result := 0;
 if DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime) and
   LocalFileTimeToFileTime(LocalFileTime, FileTime) and
   SetFileTime(Handle, @FileTime, @FileTime, @FileTime) then Exit;
 Result := GetLastError;
end;

procedure TForm1.Button1Click(Sender: TObject);
var f: Integer;
begin
 f := FileOpen('c:\myfiles\test.txt', fmOpenReadWrite);
 FileSetCreationDate(f, DateTimeToFileDate(EncodeDate(1998, 12, 18)));
 FileClose(f);
end;
0
 

Author Comment

by:frangers99
ID: 6224057
wow that was quick! thanks. Only problem is it doesnt change the last accessed property...is that possible?
0
 

Author Comment

by:frangers99
ID: 6224073
wow that was quick! thanks. Only problem is it doesnt change the last accessed property...is that possible?
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6224093
Hmmm.... it should change it....
0
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.

 
LVL 6

Expert Comment

by:edey
ID: 6225350
i wonder if fileClose(f) isn't overwriting the last accessed attr?

GL
Mike
0
 
LVL 6

Expert Comment

by:edey
ID: 6225367
ugh, didn't quite finish my thought :).  If setFileTime needs a file handle created with GENERIC_WRITE access then closing the handle will almost certainly update the access rec.  Quickly perusing the win32.hlp I don't seen another api for setting file times.  I ams wondering if you might have to open the files, change the system date/time, close the file & restore the system date/time.  A real kludge, and not really a good practice, perhaps someone else can think of a better way - if I'm not completely wrong here.

GL
Mike
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 6225578
My code works OK. What happens is that, when you open the properties of the file, the accessed date is updated. So this way you can't check it.

What you can do is changing the explorer view to 'details' and then add 'created' and 'accessed' to your current columns. To do that, right-click somewhere on the existing columns and select 'more' from the popup menu. Then enable the approperiate columns.
0
 

Author Comment

by:frangers99
ID: 6227394
what can i say! thanks.
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

Title # Comments Views Activity
Filemaker 14 vs Delphi Embarcadero 7 186
loading dll from external path 1 211
Downloading email attachments 2 67
oracle global variables 4 63
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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 …

896 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

18 Experts available now in Live!

Get 1:1 Help Now