目录名称是什么'.' 和'..'的意思是faDirectory是什么意思?

Ale*_*ith 5 delphi delphi-7

我有一个程序,用于搜索用户在路径和子路径中输入的文件,除了这一行,我对它的大部分内容都有很好的理解:

if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..')
Run Code Online (Sandbox Code Playgroud)

整个过程如下,帮助将被欣赏,因为我不确定这行代码的目的,是它检查子路径中的东西?

procedure TfrmProject.btnOpenDocumentClick(Sender: TObject);
begin
FileSearch('C:\Users\Guest\Documents', edtDocument.Text+'.docx');
end;

procedure TfrmProject.FileSearch(const Pathname, FileName : string);
var Word : Variant;
    Rec  : TSearchRec;
    Path : string;
begin
Path := IncludeTrailingBackslash(Pathname);
if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0
then repeat Word:=CreateOLEObject('Word.Application');
  Word.Visible:=True;
  Word.Documents.Open(Path + FileName);
   until FindNext(Rec) <> 0;
FindClose(Rec);


if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
 try
   repeat
   if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then
     FileSearch(Path + Rec.Name, FileName);
  until FindNext(Rec) <> 0;
 finally
 FindClose(Rec);
end;

end; //procedure FileSearch
Run Code Online (Sandbox Code Playgroud)

RRU*_*RUZ 10

1)faDirectory attibute指示条目是否是目录.

 (Rec.Attr and faDirectory) <> 0 //check if the current TSearchRec element is a directory
Run Code Online (Sandbox Code Playgroud)

2)每个目录都有两个点目录名称,在递归扫描中必须避免使用.

(Rec.Name<>'.') and (Rec.Name<>'..') //check the name of the entry to avoid scan when is `.` or `..`
Run Code Online (Sandbox Code Playgroud)

换句话说,该行表示:仅扫描当前条目是否为目录而不是a Dot Directory.

  • 不,行`(Rec.Attr和faDirectory)`使用`AND`操作数来检查条目属性中是否设置了'faDirectory`($ 00000010)值. (4认同)