5

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

if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..')

整个过程如下,如果我不确定这行代码的用途,我将不胜感激,它是否检查了子路径中的某些内容?

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
4

1 回答 1

10

1) faDirectory属性指示条目是否为目录。

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

2)每个目录有两个Dot Directory Names,递归扫描时必须避免。

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

换句话说,该行意味着:仅扫描当前条目是目录而不是Dot Directory.

于 2012-04-19T23:35:04.020 回答