Title: Filesearch function - Searching a file anywhere (a simple way)
Question: How to search a file ( a simple way)
Answer:
Searching a file anywhere ....(Filesearch Function)
By: Pooia Lalbakhsh
MS in Computer Engineering
Pooia2791@ftml.net
This simple code enables you to find (search) a special file (passing it's name to filesearch procedure as FileName variable)
within a special path ( passing the path to filesearch procedure as PathName variable) and allowing the procedure to
search in the included directories ( by setting InDir parameter into 1).
The results are added to the list variable.
uses
SysUtils,
Classes;
procedure FileSearch(const PathName, FileName : string; const InDir : boolean; list : Tstrings);
var Rec : TSearchRec;
Path : string;
begin
Path := IncludeTrailingBackslash(PathName);
if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 then
try
repeat
List.Add(path+rec.Name);
until FindNext(Rec) 0;
finally
FindClose(Rec);
end;
If not InDir then Exit;
if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
try
repeat
if (Rec.Name'.') and (Rec.Name'..') then
FileSearch(Path + Rec.Name, FileName, true, list);
until FindNext(Rec) 0;
finally
FindClose(Rec);
end;
end; //procedure FileSearch