Solved

Get file size

Posted on 2003-10-24
7
623 Views
Last Modified: 2010-04-05
I'm sending binary files from a server to clients using sockets (much like simple ftp srv). I'd like the clients to show progressbar of the current download.

Because of this, I'd like the server to send file details (size most importantly) to the client before sending the binary. How do I get the details and send? I know about filesize(..), but it seams strange having to open and close the file before sending, just to have it opened again by the steam?

if FileExists (strFile) then
begin
     // Find and send file details before sending file...    
      Socket.SendStream (TFileStream.Create (
        strFile, fmOpenRead or fmShareDenyWrite));
end;

Any good ideas?
0
Comment
Question by:Peter_
  • 3
  • 2
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 200 total points
Comment Utility
Instead of using FileExists, use FindFirst() instead... This function not only tells you if the file exists, you also get a nice TSearchRec record on succes full useful information. Something like this:

var SearchRec:TSearchRec;
begin
  if (FindFirst(strFile, faAnyfile-faDirectory, SearchRec)=0) then begin
    // File found.
    Size := SearchRec.Size;
    .
    .
    .
    FindClose(SearchRec);
  end;
0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
Oh, before I forget. If you open a TFileStream or other stream type, the stream type also includes a Size property...
0
 
LVL 4

Expert Comment

by:Colin_Dawson
Comment Utility
Don't use the TSearchRec.Size, it's limited to the 2Gb file size.  You can use this instead to allow for bigger files.....

Var
  lvFileSize : Int64;
Begin
  lvFileSize := ( Int64( SearchRec.FindData.nFileSizeHigh ) shl 32 ) + SearchRec.FindData.nFileSizeLow;
End;
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

by:alsantos
Comment Utility
Hi Peter, Try this code, I think that it can be helpful.

var
  FromFile: file of byte;
  Source: String; // string that have the true path to file
  FileLength: longint;
begin
  if FileExists (strFile) then
  begin
    AssignFile(FromFile, Source);
    reset(FromFile);
    FileLength := FileSize(FromFile); // now FileLength have the size of the file
    CloseFile(FromF);
    // Here you already know the FileLength, So, You can send the Length to client
    ...

end;

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
Comment Utility
Replace Source for strFile, and delete the var Source . You already use another var to do this. (strFile)
0
 
LVL 4

Expert Comment

by:Colin_Dawson
Comment Utility
alsantos.  Again, you're limited by the 2Gb file size!
0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
I don't think the 2 GB size is a real limitation when you're trying to send over a file over a socket to another system. Sending over files this big is a bit slow...

And yes, the TSearchRec also has a link (FindData) to a TWin32FindData record for the larger files. Quite useful sometimes.

And the Stream.Size property is an Int64 type. So if you use:

var
  MyStream:TStream;
begin
  if FileExists (strFile) then begin
    MyStream := TFileStream.Create (strFile, fmOpenRead or fmShareDenyWrite);
    // File size = MyStream.Size.
    Socket.SendStream (MyStream);
  end;
end;

Then you don't even have retrieve the file size in any way...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now