查找文件、删除文件夹

问题: 很简单的问题(RD.Attr and faDirectory)=faDirectory等于(RD.Attr=faDirectory)嘛?

来源:http://www.delphibbs.com/delphibbs/dispq.asp?lid=2497760

来自: 3333W, 时间: 2004-03-11 16:49:00, ID: 2497760 
我一直不明白啊,如果相等的话为什么不写成后一种,如果不等的话麻烦前辈们告知为什么?
如果你也不懂就帮助up一下。(请不要 硬 翻译语句的意思。谢谢合作)

来自: yostgxf, 时间: 2004-03-11 19:48:03, ID: 2497919 
不相等的.
1.faDirectory是一个文件属性值,其它还有
{ File attribute constants }

faReadOnly  = $00000001 platform;
faHidden    = $00000002 platform;
faSysFile   = $00000004 platform;
faVolumeID  = $00000008 platform;
faDirectory = $00000010;
faArchive   = $00000020 platform;
faAnyFile   = $0000003F;

2.RD.Attr 的取值是一个集合 可能是一个属性值或多个属性值

(RD.Attr and faDirectory)=faDirectory表示RD.Attr具有faDirectory属性值,当然可能还有其它的

(RD.Attr=faDirectory)表示只有faDirectory属性值, 不能有其它的

RD.Attr 可能是下面几种的组合
faReadOnly  = $00000001 platform;
faHidden    = $00000002 platform;
faSysFile   = $00000004 platform;
faVolumeID  = $00000008 platform;
faDirectory = $00000010;
faArchive   = $00000020 platform;
faAnyFile   = $0000003F;

比如说既只读,又隐藏  这样RD.Attr的取值不是faReadOnly 也不是faHidden.而是他们的组合  RD.Attr=$00000003
所以需要位操作才能判断


// 删除整个目录中的空目录, DelRoot 表示是否删除目录本身
procedure DelEmptyTree(Dir: string; DelRoot: Boolean = True);
var
  sr: TSearchRec;
  fr: Integer;
begin
  fr := FindFirst(AddDirSuffix(Dir) + '*.*', faDirectory, sr);
  try
    while fr = 0 do
    begin
      if (sr.Name <> '.') and (sr.Name <> '..') and (sr.Attr and faDirectory = faDirectory) then
      begin
        SetFileAttributes(PChar(AddDirSuffix(Dir) + sr.Name), FILE_ATTRIBUTE_NORMAL);
        DelEmptyTree(AddDirSuffix(Dir) + sr.Name, True);
      end;
      fr := FindNext(sr);
    end;
  finally
    FindClose(sr);
  end;

  if DelRoot then
    RemoveDir(Dir);
end;

原文地址:https://www.cnblogs.com/railgunman/p/1873119.html