Solved

Get file size

Posted on 2003-10-24
7
624 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

911 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

20 Experts available now in Live!

Get 1:1 Help Now