المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : حساب حجم الملفات بـ ( Byte / KB /MB /GB / TB)


Lam.Abdeldjalil
21-02-2011, 07:36 PM
السلام عليكم و رحمة الله

توجد عدة طرق لحساب حجم الملفات نقدم إثنان منهما :
الأولى : مايميزها أنها تحسب الحجم حتى بالتيرابايت TB ( لم أجربها * لا أملك ملف بها الحجم )

function FileSizeStr ( filename: string ): string;
const
K = Int64(1024); // KB
M = K * K; // MB
G = K * M; // GB
T = K * G; // TB
var
size: Int64;
handle: integer;
begin
handle := FileOpen(filename, fmOpenRead);
if handle = -1 then
result := 'Unable to open file ' + filename
else try
size := FileSeek ( handle, Int64(0), 2 );
if size < K then result := Format ( '%d bytes', [size] )
else if size < M then result := Format ( '%f KB', [size / K] )
else if size < G then result := Format ( '%f MB', [size / M] )
else if size < T then result := Format ( '%f GB', [size / G] )
else result := Format ( '%f TB', [size / T] );
finally
FileClose ( handle );
end;
end;
مثال للإستدعاء

ShowMessage('File Size '+ FileSizeStr('debian-6.0.0-i386-DVD-1.iso'));
الثانية: ما يعيبها أنها لا تعمل جيدا مع ملف ذو حجم أكبر من 2 GB


function FileSizeStr2 ( filename: string ): string;
var
f : File of byte;
fs : cardinal;
const
K = Int64(1024); // KB
M = K * K; // MB
G = K * M; // GB
T = K * G; // TB
begin
AssignFile(f,filename);
try
Reset(f);
fs := system.FileSize(f);

if fs < K then result := Format ( '%d bytes', [fs] )
else if fs < M then result := Format ( '%f KB', [fs / K] )
else if fs < G then result := Format ( '%f MB', [fs / M] )
else if fs < T then result := Format ( '%f GB', [fs / G] )
else result := Format ( '%f TB', [fs / T] );
finally
CloseFile(f);
end;
و السلام عليكم

kachwahed
22-02-2011, 04:07 PM
وعليكم السلام ورحمة الله وبركاته
فكرة جميلة :)
هناك توابع API Windows جاهزة لحساب حجم الملفات بـ KB أو B

(* -----------------------------------------
Use this function to display the "69.1 Kb"
instead 70846, "1.61 Mb" instead 1697109,
etc...
----------------------------------------- *)

// API declarations
function StrFormatByteSize(dw: DWORD; szBuf: PChar; uiBufSize: UINT): PChar; stdcall; external 'shlwapi.dll' name 'StrFormatByteSizeA';
function StrFormatKBSize(qdw: LONGLONG; szBuf: PChar; uiBufSize: UINT): PChar; stdcall; external 'shlwapi.dll' name 'StrFormatKBSizeA';

// ---------
// Code
// ---------

// ------------------------------------------------------- //
function Formatted_FileSize(SizeInBytes: Cardinal) : String;
var
arrSize: array[0..255] of Char;
begin
// same formating like in statusbar of Explorer
StrFormatByteSize(SizeInByte, arrSize, Length(arrSize) - 1);
Result := Trim(arrSize);
end;
// ------------------------------------------------------- //
function KB_Formatted_FileSize(SizeInKB: Cardinal) : String;
var
arrSize: array[0..255] of Char;
begin
// same formating like in Size column of Explorer in detailed mode
StrFormatKBSize(SizeInKB, arrSize, Length(arrSize)-1);
Result := Trim(arrSize);
end;
// ------------------------------------------------------- //
الكود لـ Mike Shkolnik
لم أجربه الآن، أظن كان به خلل صغير
بالتوفيق.