Solved

Get file size

Posted on 2003-10-24
7
629 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 200 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
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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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