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


本文整理自网络,侵删。

 






























注意函数返回值类型是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;参考:

1.File Size - Get the Size of a File in Bytes using Delphi 
http://delphi.about.com/od/delphitips2008/qt/filesize.htm 

相关阅读 >>

Delphi getversionstring 获取文件版本信息

Delphi 随机程序名

Delphi 官方 processmessages 用法代码例子

Delphi split 方法使用

Delphi xe5 中tmemo控件的应用――for android

Delphi与正则表达式

Delphi webbroker isapi 示例说明

Delphi比较两个位图是否相同

Delphi 程序嵌入桌面效果的实现

Delphi urldownloadtofile 实现文件下载

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...