如何得到指定目录下(含目录)所有文件占用的磁盘空间大小?

解决方案 »

  1.   

    procedure mythread.filesize(soursefile:string);
    var
      sr:TsearchRec;
    begin
      soursefile:=IncludeTrailingBackslash(soursefile);
      if findfirst(soursefile+'*.*',faanyfile,sr)=0 then
      begin
        repeat
          if (sr.Name<>'.') and (sr.Name<>'..') then
          begin
            if sr.Attr<>fadirectory then
            begin
              totalsize:=totalsize+sr.Size;
            end
            else
            begin
              filesize(soursefile+sr.Name);
            end;
          end;
        until  findnext(sr)<>0;
      end;
    end;
      

  2.   

    function GetDirectorySize(const ADirectory: string): Integer; var   Dir: TSearchRec;   Ret: integer;   Path: string; begin   Result := 0;   Path := ExtractFilePath(ADirectory);   Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir);    if Ret ?? NO_ERROR then   exit;    try   while ret=NO_ERROR do   begin   inc(Result, Dir.Size);   if (Dir.Attr in [faDirectory]) and (Dir.Name[1] ?? '.') then   Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*'));   Ret := Sysutils.FindNext(Dir);   end;   finally   Sysutils.FindClose(Dir);   end; end;