这是一个算目录大小的函数,在下的问题在程序中,请各位看官不吝赐教:Function TForm1.GetDirSize(Dir:string; subdir:boolean):longint;
var
  rec:TSearchRec;
  Found:integer;begin
  result := 0;
  if dir[length(dir)] <> '\' then dir := dir + '\' ;
  Found := findfirst(dir + '*.*',faAnyFile,rec);
  while Found = 0 do
  begin
    inc(result,rec.Size);
    //我知道以下3行是算子目录的大小,如果有两个子目录,程序好像只能算一个目录的大小
      但我试了一下,计算的值没有问题,那么程序是怎么算的呢?
    if (rec.Attr and faDirectory > 0) and (rec.Name[1] <> '.') and SubDir = true  then
    inc(result,GetDirSize(dir + rec.Name,true));
    found := findnext(rec);
  end;
  FindClose(rec);
end;我看了一下帮助,Tsearchrec没有说明

解决方案 »

  1.   

    这是个递归函数
    注意:inc(result,GetDirSize(dir + rec.Name,true));在这里GetDirSize自己调用自己了(递归),一般树形的数据结构经常会采用递归来处理
      

  2.   

    TSearchRec defines file information searched for by FindFirst or FindNext.UnitSysUtilsDelphi syntax:On Windows:   type 
       TSearchRec = record
        Time: Integer;
        Size: Integer;
        Attr: Integer;
        Name: TFileName;
        ExcludeAttr: Integer;
        FindHandle: THandle;
        FindData: TWin32FindData;
    end;On Linux:   type TSearchRec = record
        Time: Integer;
        Size: Integer;
        Attr: Integer;
        Name: TFileName;
        ExcludeAttr: Integer;
        Mode: mode_t;
        FindHandle: Pointer;
        PathOnly: string;
        Pattern: string;
    end;
    DescriptionThe TSearchRec type defines file information searched for by a FindFirst or FindNext function call. If a file is found, the fields of the TSearchRec type parameter are modified to specify the found file.Attr represents the file attributes of the file. Test Attr against the following attribute constants or values to determine if a file attribute matches the file's properties.On Windows, attribute constants map directly to DOS file attributes. 
    Constant Value Description Linux Meaning
    faReadOnly 1 Read-only files Current user does not have write access.
    faHidden 2 Hidden files File name begins with ".".
    faSysFile 4 System files File is socket, symbolic link, device file, or FIFO.
    faVolumeID 8 Volume ID files Not used.
    faDirectory 16 Directory files Directory.
    faArchive 32 Archive files Not used.
    faSymLink 64 Symbolic link File is a symbolic link.
    faAnyFile 71 Any file Any file.
    Note:The faReadOnly constant has the same name as the enumerated value that is defined by the TFieldAttribute type. If both the SysUtils and the Db units are used in your source files, you must disambiguate by specifying the unit to qualify the use of faReadOnly. That is, write SysUtils.faReadOnly (Delphi) or SysUtils::faReadOnly (C++).To test for an attribute, combine the value of the Attr field with the attribute constant with the and operator. If the file has that attribute, the result will be greater than 0. For example, if the found file is a hidden file, the following expression will evaluate to true: (SearchRec.Attr and faHidden) <> 0.