Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi Function to return Last File Access DateTime Stamp

Posted on 2006-07-21
13
Medium Priority
?
620 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 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
Technology Partners: 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

927 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