Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to determine the size of a file Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
09-Oct-02
Category
Files Operation
Language
Delphi All Versions
Views
96
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How to determine the size of a file

Answer:

Solve 1:

You can use the type TSearchRec as follows:

1   function LoadSize(Path: string): integer;
2   var
3     Rec: TSearchRec;
4   begin
5     Result := 0;
6     if FindFirst(Path, faAnyFile, Rec) = 0 then
7     begin
8       Result := Rec.Size;
9       FindClose(Rec);
10    end;
11  end;



Solve 2:

12  { ... }
13  var
14    fileInfo: _WIN32_FILE_ATTRIBUTE_DATA;
15    totalSize: Int64;
16  begin
17    GetFileAttributesEx(PChar(EdtPath.Text), GetFileExInfoStandard, @fileInfo);
18    totalSize := fileInfo.nFileSizeHigh shl 32 or fileInfo.nFileSizeLow;
19  end;



Solve 3:

20  { ... }
21  var
22    SR: TSearchRec;
23    FileName: string;
24    r: integer;
25  begin
26    FileName := 'c:\winnt\system32\shell32.dll';
27    r := FindFirst(FileName, faAnyFile, SR);
28    if r = 0 then
29    begin
30      Label1.Caption := Format('Size of %s is %d bytes (%0.1f Mb)',
31        [FileName, SR.Size, Sr.Size / 1000000]);
32      FindClose(SR);
33    end
34    else
35      Label1.Caption := 'File does not exist';
36  end;



Solve 4:

37  procedure TForm1.Button1Click(Sender: TObject);
38  var
39    hFile: THandle;
40    Size: Integer;
41  begin
42    if OpenDialog1.Execute then
43    begin
44      hFile := FileOpen(OpenDialog1.FileName, fmOpenRead or fmShareDenyNone);
45      Size := GetFileSize(hFile, nil);
46      {CloseHandle: use to close handle created with CreateFile which is 
47  		what FileOpen calls internally}
48      CloseHandle(hFile);
49      ShowMessage(Format('Size in bytes: %d', [Size]));
50    end;
51  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC