Solved

Delphi Function to return Last File Access DateTime Stamp

Posted on 2006-07-21
13
603 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 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