What do directory names '.' and '..' mean and what does faDirectory mean?
Asked Answered
C

1

5

I have got a procedure which searches for a file entered by the user in a path and subpaths, i have a good understanding of the most of it except for this line:

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

The whole procedure is as follows, help would be appreciated as im not sure exactly of the purpose of this line of code, is it checking something in the subpath?.

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
Carpogonium answered 19/4, 2012 at 23:30 Comment(0)
R
10

1) The faDirectory attibute indicates whether the entry is a directory.

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

2) Each directory has two Dot Directory Names, which must be avoided in the recursive scan.

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

In other words that line means: only scan if the current entry is a directory and is not a Dot Directory.

Rheumatism answered 19/4, 2012 at 23:35 Comment(5)
so (Rec.Attr and faDirectory) returns a negative value if it the current TSearchRec element is a directory? why is thisCarpogonium
No, the line (Rec.Attr and faDirectory) uses the AND operand to check if the faDirectory($00000010) value is set in the attibutes of the entry.Rheumatism
i see, thanks a lot. I know that this was not the original question i should technically create a new question, but i was wondering if you have the time whether you could suggest to me how i could have a showmessage to indicate that i file has not been found, i have attempted to put a boolean variable where the variable FileFound is set to false, but FileFound := true if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0, however being a recursive procedure this wont work, any easy methods of achieving it?Carpogonium
You can have FileSearch() return a Boolean indicating whether it finds the file on any given iteration. Once an iteration returns True, keep passing True up the recersive chain to the original caller. FYI, on XP and later, there is a SearchPath() function available that searches for a filename in a specified path so you don't have to search manually anymore.Tribalism
i've done this and it works except for the fact that when the showmessage comes up and i press ok it keeps reappearing even though ive got it at the end of the procedureCarpogonium

© 2022 - 2024 — McMap. All rights reserved.