0

使用教程中的代码并进行各种修改我得到了递归过程的工作代码,该过程搜索具有文件名的文件,当参数从另一个过程传递时,用户在给定路径和通过子文件夹输入文件名单击按钮。如下:

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

在尝试了解正在发生的事情之后,直到第一个 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;

我的猜测是,第一行是检查在给定路径中是​​否找到任何子文件夹,但我不太确定其余的以及它是如何工作的,如果那是正确的。帮助将不胜感激。

4

0 回答 0