怎样用比较简洁的方法取得一个目录的大小(包括其所有子目录和文件)?最好运行速度别太慢。

解决方案 »

  1.   

    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;
      

  2.   

    请 jiaai(很丑也不温柔)将??写成可运行的代码好吗?
      

  3.   

    Here's a bit of code I use to get the total size of a given directory and all it's sub directories.  I know you didn't ask for this, but I thought it maybe useful to know :)function GetDirectorySize(Dir: String): Double;
    var
     SR: TSearchRec;
     I: Integer;
    begin
     Dir := IncludeTrailingBackSlash(Dir); I := FindFirst(Dir + '*.*', faAnyFile, SR);
     while I = 0 do
       begin
         if (SR.Name[1] <> '.') then //exclude '.' and '..' directories
           begin
             if (SR.Attr and faDirectory) = faDirectory then
               Result := Result + GetDirectorySize(Dir + SR.Name)  //calculate size of nested directory
             else
               Result := Result + SR.Size;
           end; {if Name <> '.'}
         I := FindNext(SR);
       end; {while I = 0} FindClose(SR);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
     ShowMessage(Format('Total Size of Directory is: %n bytes', [GetDirectorySize('D:\Programming')]));
    end;