Solved

Delphi Function to return Last File Access DateTime Stamp

Posted on 2006-07-21
13
597 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
Comment Utility
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
Comment Utility
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
Comment Utility
Alfa File Monitor 2003 is free !

2004 costs $1999 :O
0
 

Author Comment

by:whitmark
Comment Utility
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
Comment Utility
Sorry, didn't see your Alfa File Monitor comments . . . will this aleviate the issue I mentioned above?
0
 
LVL 16

Expert Comment

by:CodedK
Comment Utility
:)
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 16

Expert Comment

by:CodedK
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks sun4sunday / codedk for your contributions!
0
 
LVL 16

Expert Comment

by:CodedK
Comment Utility
Thank you for the points and the grade.
I'm glad i could help.
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

Suggested Solutions

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

14 Experts available now in Live!

Get 1:1 Help Now