Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi Function to return Last File Access DateTime Stamp

Posted on 2006-07-21
13
Medium Priority
?
617 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
[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
  • 6
  • 4
  • 2
13 Comments
 
LVL 16

Accepted Solution

by:
CodedK earned 1800 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 1800 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
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!

 

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 200 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

670 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