• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 635
  • Last Modified:

Get file size

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
Peter_
Asked:
Peter_
  • 3
  • 2
  • 2
1 Solution
 
Wim ten BrinkCommented:
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
 
Wim ten BrinkCommented:
Oh, before I forget. If you open a TFileStream or other stream type, the stream type also includes a Size property...
0
 
Colin_DawsonCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
alsantosCommented:
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
 
alsantosCommented:
Replace Source for strFile, and delete the var Source . You already use another var to do this. (strFile)
0
 
Colin_DawsonCommented:
alsantos.  Again, you're limited by the 2Gb file size!
0
 
Wim ten BrinkCommented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now