这是一个算目录大小的函数,在下的问题在程序中,请各位看官不吝赐教: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没有说明
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没有说明
解决方案 »
- delphi网页交互,获取当前网页下所有图片的链接
- 有一定挑战性:代码中执行一个命令行exe文件,这个exe文件在控制台输出的字符串,有什么办法可以获得吗?
- 请给我个线程中增加记录到数据库的例子,谢谢!
- too many actual parameters?
- 一个关于FastReport3的问题????
- 关于下面这篇文章里的疑问(关于OFFICE COM ADDIN)
- 含FastReport2.5的数据库程序的安装问题?
- [microsoft][odbc sql server driver]没有执行可选特性
- 急受
- 高分悬赏!
- 菜鸟提问 望各位大虾多多帮忙哈
- delphi和sql server的关系
注意:inc(result,GetDirSize(dir + rec.Name,true));在这里GetDirSize自己调用自己了(递归),一般树形的数据结构经常会采用递归来处理
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.