حساب حجم الملفات بـ ( Byte / KB /MB /GB / TB)
#1
السلام عليكم و رحمة الله

توجد عدة طرق لحساب حجم الملفات نقدم إثنان منهما :
الأولى : مايميزها أنها تحسب الحجم حتى بالتيرابايت 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;
و السلام عليكم
الرد


الردود في هذا الموضوع
حساب حجم الملفات بـ ( Byte / KB /MB /GB / TB) - بواسطة Lam.Abdeldjalil - 21-02-2011, 08:36 PM

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم