2012-04-19 154 views
5

我有其搜索用户在路径和子路径进入文件的过程中,我有最的一个很好的了解,除了这行:什么是目录名'。'和'..'是什么意思,faDirectory是什么意思?

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 

回答

10

1)faDirectory attibute指示该条目是否是目录。

(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扫描。

+0

so(Rec.Attr和faDirectory)如果当前TSearchRec元素是一个目录,则返回一个负值?为什么这是 – Alexjjsmith 2012-04-19 23:51:38

+4

不,行'(Rec.Attr和faDirectory)'使用'AND'操作数来检查faDirectory'($ 00000010)值是否在条目的属性中设置。 – RRUZ 2012-04-19 23:55:51

+0

我明白了,非常感谢。我知道这不是我应该在技术上创建一个新问题的原始问题,但我想知道如果你有时间,是否可以向我建议我怎么可以有一个showmessage来表明我没有找到我的文件,我有试图把变量FileFound设置为false的布尔变量,但是如果FindFirst(Path + FileName,faAnyFile-faDirectory,Rec)= 0,FileFound:= true,但是这是一个不可用的递归过程,任何简单的实现方法它? – Alexjjsmith 2012-04-20 00:19:13