0

using code from a tutorial and making various modifications i have got working code for a recursive procedure which searches for a file with a file name which has been entered by the user at a given path and through sub folders when the parameters are passed from another procedure at a button click. it is as follows :

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

After trying to learn what is happening of i have gained a good understanding up until the point of the first FindClose(Rec), however this section of code i'm still unsure of :

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;

my guess is that the first line is checking whether there are any subfolders found in the given path, but i'm not too sure on the rest and how it works if that is even correct. help would be appreciated.

Jeowkes
  • 501
  • 7
  • 20
  • you are right, second block of code looks for subfolders in given path with `*.*` search pattern (idk why there is such mask). then it skips current folder (`.`) and parent folder `..`. After that, it runs recursive search for `*.docx` file in found subdirectory. You can remove `rec.attr and faDirectory` in `if`-statement, because it is used in `FindFirst`-call. – teran Apr 17 '12 at 13:21
  • Believe it or not the faDirectory atttribute used in the FindFirst call does _not_ actually limit the results to directories so the check in the if statement _is_ still needed. – Mike W Apr 17 '12 at 13:36
  • 1
    Which part do you not understand? Please be more specific. Stack Overflow isn't intended to be a "read my mind" site where *all* your questions are answered in a single post. Ask a specific question about the part of the code you don't understand. If there are lots of parts you don't understand, then you can ask several questions; or maybe this isn't the code you should be using for whatever your task is. – Rob Kennedy Apr 17 '12 at 16:04
  • okay its this line particularly if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..') then FileSearch(Path + Rec.Name, FileName); – Jeowkes Apr 17 '12 at 17:05
  • Besides what Rob said... You are aware that you're creating multiple instances of Word by putting the `CreateOLEObject` inside the loop, and then orphaning them in memory by reusing the variable again every pass, right? (You can confirm this by running your app in a folder with multiple Word docs, so that multiples get opened, and then exiting your app and closing Word. Look at Task Manager's Process tab, and you'll see several copies of Word.exe hanging around.) – Ken White Apr 17 '12 at 17:10
  • oh right, would it be better to place a then begin where there is a there repeat, then put the Word:=CreateOLEObject('Word.Application'); and then the repeat loop – Jeowkes Apr 17 '12 at 17:36
  • I'd put it between the `if FindFirst(...) then` and `repeat`, so it gets called only the first time, just before the `repeat` loop starts (but after you know a Word doc exists). – Ken White Apr 17 '12 at 18:33
  • ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..') so what is this checking exactly? – Jeowkes Apr 19 '12 at 01:45

0 Answers0