Whats the best way of getting the 3 Date & Time Files of a File.

Hi Experts!?

I'd Like to know your opinions on the best, shortest & quickest route to display all the Date & Times.

The 3 Dates & Times am after are Created, Modified & Accessed.


I'd be calling them calling from a Function, which is like..
memo1.lines.add('Created: '+GetDate(file, 1, long));
1=Created
2=Modified
3=Accessed
4=Created & Modified
ect..

should I use Case or If for that function?

but getting back to the Date & Time of the file.

I'd like to be able to display them in there true long format and short format.

long = Tuesday, February 14, 2006, 09:50:19
short = 14/02/06 09:50:19


Thanks
4R.
LVL 4
4RabbitsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
a sample

procedure TForm1.Button1Click(Sender: TObject);
var
  FHandle : THandle;       //FileHandle
  AT,CT,WT : TSystemTime;  //SystemTimeTypes
  A,C,W : TFileTime;       //FileTimeTypes
  LE : Integer;            //ErrorCode
begin
  if opendialog1.execute then
  begin
    //Open The File
    Fhandle:=createfile(Pchar(OpenDialog1.FileName),
                        0,
                        FILE_SHARE_READ,
                        nil,
                        OPEN_EXISTING,
                        SECURITY_ANONYMOUS,
                        0);
    try
      //Get the Times
      if GetFileTime(FHandle,@C,@A,@W) then
      begin
        //Some Conversions-
        FileTimeToSystemTime(C,CT);  //CreationTime
        FileTimeToSystemTime(A,AT);  //LastAccessTime
        FileTimeToSystemTime(W,WT);  //LastWriteTime
        //Output
        Edit1.Text := DateTimeToStr(SystemTimeToDateTime(CT)); //CreationTime
        Edit2.Text := DateTimeToStr(SystemTimeToDateTime(AT)); //LastAccessTime
        Edit3.Text := DateTimeToStr(SystemTimeToDateTime(WT)); //LastWriteTime
      end
      else
      begin
        // ? any error
        LE := GetLastError;
        Showmessage(inttostr(LE));
      end;
    finally
      FileClose(FHandle);
    end;
  end;
end;

meikl ;-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AmigoJackCommented:
function getDateTimeString(ft1: filetime; iDateType: DWORD= 0): string;
var
  st1: systemtime;
  s1: string;
  p1: pchar;
begin
  getmem(p1, 300);

  filetimetolocalfiletime(elem^.tTime, ft1);
  filetimetosystemtime(ft1, st1);
  getdateformat(LOCALE_USER_DEFAULT, iDateType, @st1, nil, p1, 300);
  s1:= strpas(p1)+ ' ';
  gettimeformat(LOCALE_USER_DEFAULT, 0, @st1, nil, p1, 300);
  s1:= s1+ strpas(p1);
  item.SubItems.Add(s1);

  freemem(p1);
  result:= s1;
end;

procedure listFileTimes(fname: string; DWORD iDateType= 0);
var
  wfd: win32_find_data;
  h1: thandle;
begin
  h1:= findfirstfile(pchar(fname), wfd);
  if h1= INVALID_HANDLE_VALUE then exit;

  with form1.memo1.lines do begin
    add('last write: '+ getDateTimeString(wfd.ftLastWriteTime, iDateType));
    add('created: '+ getDateTimeString(wfd.ftCreationTime, iDateType));
    add('last access: '+ getDateTimeString(wfd.ftLastAccessTime, iDateType));
  end;
  windows.findclose(h1);
end;




you should try the example with a memo1 on your form. call it like this

  listFileTimes('c:\boot.ini', DATE_LONGDATE);
  listFileTimes('c:\boot.ini', DATE_SHORTDATE);

havent tested it, but i know GetDateFormat() is working for giving the date in any format you like. plus FindFirstFile() gives you more rapid all file times. if you have any further questions dont bother :)
AmigoJackCommented:
oh yeah - i forgot GetFileTime(). that could be faster... anyway :)
4RabbitsAuthor Commented:
Thanks meikl.

AmigoJack, for some reason I couldn't get it to work :(

errors here #error#

 filetimetolocalfiletime(#elem^#.tTime, ft1);

thanks.
4R.
AmigoJackCommented:
whoops - exactly that line: delete it :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.