When working with files and folders from a Borland Delphi 7, application you sometimes need to know what is the size of a file (in bytes or non formating).
Get File Size
The FileSize function returns the size of a file in bytes, -1 if the file was not found.
The FileSize function as follows:
//Returns file size in bytes or -1 if not found.
function FileSize(fileName : wideString) : Int64;
var
sr : TSearchRec;
begin
if FindFirst(fileName, faAnyFile, sr ) = 0 then
result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow)
else
result := -1;
FindClose(sr) ;
end;
When working with files from Borland Delphi 7 you might want to display the size of a file to the user in a Explorer-like format where the file size is not displayed in bytes – but the display depends on the size of the actual file.
To most users “21720″ is confusing – where “21.21 KB” is much more user friendly.
Format Byte Size to String
A custom Delphi function, FormatByteSize, converts a byte value into a string that represents the number expressed as a size value in bytes, kilobytes, megabytes, or gigabytes, depending on the size.
Format Byte Size to String function as follows:
//Format file byte size
function FormatByteSize(const bytes: Longint): string;
const
B = 1; //byte
KB = 1024 * B; //kilobyte
MB = 1024 * KB; //megabyte
GB = 1024 * MB; //gigabyte
begin
if bytes > GB then
result := FormatFloat('#.## GB', bytes / GB)
else
if bytes > MB then
result := FormatFloat('#.## MB', bytes / MB)
else
if bytes > KB then
result := FormatFloat('#.## KB', bytes / KB)
else
result := FormatFloat('#.## bytes', bytes) ;
end;
Command to run the functions above is:
procedure TForm1.btn1Click(Sender: TObject);
var
fsize: Integer;
fsext: String;
begin
//Variable for filesize of filename
fsize := FileSize('C:\test.txt');
//Variable for filesize format
fsext := FormatByteSize(fsize);
//Show filesize of filename
ShowMessage('File size (Non Format): ' + IntToStr(fsize));
//Show filesize format
ShowMessage('File size (With Format): ' + fsext);
end;
Once the application is started, it will appear as follows:
Result Get Filesize non Format
Result Get Filesize with Format from Get Filesize
If you want to use open dinamycs file, you can with Open Dialog.
Full Source Code:::
//******************************************//
// Sampel Source Code Learning Delphi //
// Get Filesize with Non Format and Format //
// by : G. Giarto //
// E-Mail : support@learning-delphi.com //
// Website: http://www.learning-delphi.com //
//******************************************//
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
lbl1: TLabel;
lbl2: TLabel;
lbl3: TLabel;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption := 'Get Filesize with Non Format and Format';
lbl1.Caption := Caption;
lbl2.Caption := 'Filename';
lbl3.Caption := 'C:\test.txt';
btn1.Caption := 'Get &Filesize';
btn2.Caption := 'E&xit';
end;
//Returns file size in bytes or -1 if not found.
function FileSize(fileName : wideString) : Int64;
var
sr : TSearchRec;
begin
if FindFirst(fileName, faAnyFile, sr ) = 0 then
result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow)
else
result := -1;
FindClose(sr) ;
end;
//Format file byte size
function FormatByteSize(const bytes: Longint): string;
const
B = 1; //byte
KB = 1024 * B; //kilobyte
MB = 1024 * KB; //megabyte
GB = 1024 * MB; //gigabyte
begin
if bytes > GB then
result := FormatFloat('#.## GB', bytes / GB)
else
if bytes > MB then
result := FormatFloat('#.## MB', bytes / MB)
else
if bytes > KB then
result := FormatFloat('#.## KB', bytes / KB)
else
result := FormatFloat('#.## bytes', bytes) ;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
fsize: Integer;
fsext: String;
begin
//Variable for filesize of filename
fsize := FileSize('C:\test.txt');
//Variable for filesize format
fsext := FormatByteSize(fsize);
//Show filesize of filename
ShowMessage('File size (Non Format): ' + IntToStr(fsize));
//Show filesize format
ShowMessage('File size (With Format): ' + fsext);
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
Application.Terminate;
end;
end.
-=Thank.you=- How To Make Get Filesize with Non Format and Format with Borland Delphi 7
0 comments:
Post a Comment