File Size, Date, Time Component

I need a Component that returns the Size, Date, and Time from a given file.
LlysanderAsked:
Who is Participating?
 
LischkeConnect With a Mentor Commented:
..and here I am :-) Llysander you can use the "accept comment as answer" link near every comment here...

Ciao, Mike
0
 
florisbCommented:
Could help:

no source:
http://24.112.41.38/delphideluxeii/d20f3.htm
fileseek.zip (3,369 bytes) A file scanner that returns the following from the found file: Filename, Filetime and Filesize ( ver. 1.0, added 26-Jan-98, no src., I could Delphi 1 or 3 it!! )   by Todd Frankson


Good luck,
Floris.
0
 
LischkeCommented:
Hi,

essentially, you don't need a component. Just call FindFirst with the full path of the file. The returned search record contains all you need. With the FindData record in the search record you have even more information. Don't forget to call FindClose if FindFirst was successfull.

Ciao, Mike
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
edeyCommented:
You could look at the delphi functions:

AssignFile
fileGetDate
fileDateToDateTime
FileSize



Gl
Mike
0
 
AJFlemingCommented:
Try this unit :

It allows you to get the size,  times for a named file.

regards,

Adam..

unit FileOperations;

interface

function GetNamedFileSize(Filename : string) : int64; forward;
function GetNamedFileDate(Filename : string) : tDateTime; forward;

implementation

uses
  Windows,
  SysUtils;

function GetNamedFileSize(Filename : string) : int64;
var
  FileHandle : cardinal;

begin
  FileHandle := CreateFile(PChar(filename),
                           GENERIC_READ,
                           FILE_SHARE_READ,
                           nil,
                           OPEN_EXISTING,
                           FILE_ATTRIBUTE_NORMAL,
                           0);

  if (FileHandle = INVALID_HANDLE_VALUE) then
     raise EInOutError.Create('File Handle Invalid - could not access ' + Filename);
  //get the file size using the handle
  GetNamedFileSize := GetFileSize(FileHandle,nil);
end;

function GetNamedFileDate(Filename : string) : tDateTime;

var
  SearchRec : tSearchRec;

begin
  FindFirst(Filename,faAnyFile,SearchRec);
  GetNamedFileDate := FileDateToDateTime(SearchRec.time);
  FindClose(SearchRec);
end;

end.


0
 
MadshiCommented:
I would strongly recommend Mike's (Lischke's) suggestion, since all other suggestions (didn't look at Floris' though) need to OPEN the file in order to get the informations. That's bad practise, because there will be a lot of files that you simply can't or aren't allowed to open. With Mike's suggestion you can ask the informations for EVERY file. (I'm using the same approach in my programs, too).

Mike, how about a little code example to earn the points?   :-)

Regards, Madshi.
0
 
AJFlemingCommented:
Oops, just looked more carefully at the man-pages for FindFirst... You can use that to get the file size as well... And as Madshi mentioned, it means you don't have to open the file.

cheers,

Adam...

P.S. Incidentally, I didn't just read Mikes comment and propose something which implemented it. I was reading a cached copy of the page. So since you got there first (according to time) if Llysander wants to reject my answer so that you can claim the points, that's fine with me.
0
 
edeyCommented:
Hmmm... Mr. Lischke, great answer, I never even thought of approaching it that way good job :)

As far as AJFlemming's cache comment, anybody else notice some strange stuff happening @ ee lately, I have gotten some really late comment notifications, points for my comments in unanswered q's (didn't know it was policy, is this new?)

GL
Mike
0
 
LischkeCommented:
Thank you, Madshi, for pointing out that facts. In fact, Adam already uses the FindFirst approach without thinking it to its end at first (now you have Adam :-)).

Another important fact when using FindFirst is to close the find handle only in case FindFirst was successfull. Otherwise you can hang WinNT for a minute or longer until FindClose returns. This also true for file enumeration. Hence the code should look like:

function GetFileDateAndTime(const FileName: String; var Size: Int64; var Date: TDateTime): Boolean;

var
  SR: TSearchRec;

begin
  if FindFirst(FileName, faAnyFile, SR) = 0 then
  begin
    Size := Int64(SR.FindData.nFileSizeHigh) shl 32 + SR.FindData.nFileSizeLow;
    Date := FileDateToDateTime(SR.Time);
    FindClose(SR);
    Result := True;
  end
  else Result := False;
end;


The returned size is the correct In64 value so files > 2GB are correctly identified. The returned time is the last write time. If you need other times (creation time, last access) then look at SR.FindData and the sources to FindFirst (or just ask me :-).

Ciao, Mike
0
 
LlysanderAuthor Commented:
Thanks to all for the responses.  Mike (Lischke) should get the points for his answer.  Mike if you would repost your comment as an answer.

Thanks again.
0
 
LischkeCommented:
Mike (edey), I nearly forgot to answer you :-) E-E has recently cleared all those open questions and unaccepted answers (which brought down the Delphi area from more than 750 open question, which was more than in any other forum, to about 200). And in this event all unaccepted answers were automatically accepted with a C grading.

Ciao, Mike
0
 
LlysanderAuthor Commented:
Mike,

I don't quite follow on the Accept Comment part.  I have no such links.
0
 
LischkeCommented:
..perhaps because I provided an answer? The links are usually there, believe me.

Ciao, Mike
0
 
MadshiCommented:
Yes, I think this "accept comment as answer" is only available, when nobody has locked the question with an answer before...
0
All Courses

From novice to tech pro — start learning today.