Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get file size

Posted on 2003-10-24
7
Medium Priority
?
633 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 800 total points
ID: 9613129
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
ID: 9613136
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
ID: 9613219
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

by:alsantos
ID: 9613233
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
ID: 9613245
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
ID: 9613257
alsantos.  Again, you're limited by the 2Gb file size!
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9613352
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

Independent Software Vendors: 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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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