Solved

Delphi Function to return Last File Access DateTime Stamp

Posted on 2006-07-21
13
606 Views
Last Modified: 2010-04-05
I am using Delphi 2005 and WinXP and could use a function that returns the datetime stamp for the last access of a file.  In particular, I am looking to determine the last datetime a .wav file was played (accessed) and how might I prevent an I/O error if it is currently playing (open)?  Thanks!
0
Comment
Question by:whitmark
  • 6
  • 4
  • 2
13 Comments
 
LVL 16

Accepted Solution

by:
CodedK earned 450 total points
ID: 17157786
Hi.

With this function you can get when file last access :


function GetLastAccessTime(sFileName: string): TDateTime;
var
  ffd: TWin32FindData;
  dft: DWORD;
  lft: TFileTime;
  h:   THandle;
begin
  h := Windows.FindFirstFile(PChar(sFileName), ffd);
  if (INVALID_HANDLE_VALUE <> h) then
  begin
    Windows.FindClose(h);
    FileTimeToLocalFileTime(ffd.ftLastAccessTime, lft);
    FileTimeToDosDateTime(lft, LongRec(dft).Hi, LongRec(dft).Lo);
    Result := FileDateToDateTime(dft);
  end;
end;

Hope this helps. :)
0
 
LVL 16

Assisted Solution

by:CodedK
CodedK earned 450 total points
ID: 17157826
You can use Alfa File Monitor component.

Alfa File Monitor is a Delphi component that monitors for file system access/change, and notifies accordingly.
It monitors whole system, not just a simple directory.
It does this real-time, because it uses Vxd driven file system interceptor.

http://www.aptrio.com/Development/ActiveX/alfa-file-monitor-source-code-download-1332.html
0
 
LVL 16

Expert Comment

by:CodedK
ID: 17157836
Alfa File Monitor 2003 is free !

2004 costs $1999 :O
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:whitmark
ID: 17158448
Thanks CodedK, this compiled and ran without a hitch.  I tried this out against a bunch of wav files that were programmatically played and noticed that the file last access date was not changed.   Is there any way to detect this type of programmatic access vs when someone just clicks on the file and selects play.  Thanks?
0
 

Author Comment

by:whitmark
ID: 17158472
Sorry, didn't see your Alfa File Monitor comments . . . will this aleviate the issue I mentioned above?
0
 
LVL 16

Expert Comment

by:CodedK
ID: 17159040
:)
I dont know a way but i'll search
Maybe you could copy your file to another position and try to delete it if you get an error
then its in use. Something like that :

Copyfile bla.wav
Try
deletefile bla.wav
On Exception do ShowMessgae('File is in use')
0
 
LVL 16

Expert Comment

by:CodedK
ID: 17159050
Another way is when you play your file programmatically is to change the file date to NOW before playing it...

procedure TForm1.Button2Click(Sender: TObject);  <---- Play button
var
  HFile: Word;
  MyDate: TDateTime;
  MyDate2: Integer;
begin
  HFile:=FileOpen('The file name here', fmOpenWrite);
  MyDate:=StrToDateTime('Edit1 text with the date you wish your file to be.... You can use Now function');
  MyDate2:=DateTimeToFileDate(MyDate);
  FileSetDate(HFile, MyDate2);
  FileClose(HFile);
end;
0
 
LVL 9

Expert Comment

by:sun4sunday
ID: 17160901
procedure TForm1.Button1Click(Sender: TObject);
var
   F: TSearchRec;
   m:Integer;
   Tim : TSystemTime;
begin
   m:= findfirst('c:\Test.txt',faArchive ,F);

   FileTimeToSystemTime(FileTime(f.FindData.ftLastAccessTime),Tim);
   ShowMessage('Last Access :' +IntToStr(Tim.wMonth) +'/' +IntToStr(Tim.wDay) +'/' +IntToStr(Tim.wYear));

   FindClose(F);
end;

NOT tested with D2005

sun4sunday
0
 

Author Comment

by:whitmark
ID: 17160954
These ideas might work if I had access to the program that is playing the wav files, but I don't.  I can specify the full path of the wav file but I don't have access to application that programmatically plays the wave file when certain conditions arise.  Plus, its a short wav file of only few seconds.   Its interesting that programs can access files yet not change the last access date time stamp.  Any other thoughts?
0
 
LVL 9

Assisted Solution

by:sun4sunday
sun4sunday earned 50 total points
ID: 17163263
you can use  madSecurity to get/set file accesses
www.madshi.net
then you can use TSearchRec to get the attributes of the file.

its is not free .

I never used 'Mad' until Madshi allow me to use it  :(

sun4sunday
0
 

Author Comment

by:whitmark
ID: 17175326
Thanks sun4sunday / codedk for your contributions!
0
 
LVL 16

Expert Comment

by:CodedK
ID: 17176234
Thank you for the points and the grade.
I'm glad i could help.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mobile Keyboard covers the display of the TMemo 3 96
find a node in VST 2 73
QRReport  TQrmemo vertical stretching 1 53
When i run adoquery my application freezes 26 180
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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