您的位置:首页 > 编程语言 > Delphi

[代码]Delphi实现获取文件及文件夹大小(支持超过2G的大文件)

2011-07-26 12:58 851 查看
注意函数返回值类型是Int64,如果文件存在则返回文件大小,否则返回0。

function FileSize(FileName: string): Int64;
var
sr: TSearchRec;
begin
if FindFirst(FileName, faAnyFile, sr) = 0 then
Result := Int64(sr.FindData.nFileSizeHigh) shl 32 + Int64(sr.FindData.nFileSizeLow)
else
Result := 0;

FindClose(sr);
end;


由此可以得到获取文件夹大小的函数如下:

function FolderSize(FolderName: string): Int64;
var
sr: TSearchRec;
begin
Result := 0;

if RightStr(FolderName, 1) <> '\' then FolderName := FolderName + '\';

if FindFirst(FolderName + '*.* ', faAnyFile, sr) = 0 then
repeat
if (sr.Name <> '.') and (sr.Name <> '..') then begin
Result := Result + FileSize(FolderName + sr.Name);

if (sr.Attr and faDirectory) <> 0 then
Result := Result + FolderSize(FolderName + sr.Name + '\');
end;
until FindNext(sr) <> 0;

FindClose(sr);
end;


参考:

File Size - Get the Size of a File in Bytes using Delphi

http://delphi.about.com/od/delphitips2008/qt/filesize.htm

Technorati 标签: Delphi,FindFirst,nFileSizeHigh
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: