Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search files in Dir and subdirectories 3

Status
Not open for further replies.

msgopala

Programmer
Oct 30, 2008
43
US
I have an exe that is executed using a batch file -- which extracts files to a temp file and then searches for Myfile.INI and when a match is found copies files from the temp folder to the found folder.

So far I have been able to find files using sysutils.FileSearch but this function does not search the subdirectories under the different drives.

The FindFirst, FindNext works for a directory at a time but searches subdirs as well.

How can I use Sysutils.FileSearch and be able to search the subdirectories under all the available dirs.

Thanks
 
I think the trick you are looking for is a recursive call. Rather than risk confusing you by trying to compose code off the top of my head, let me dig for some existing code from my archives.

Basically, write a function to spin thru the files. If the 'file' is a directory, call the same function from within it.

Roo
Delphi Rules!
 
agent ransack can to that for you :)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
thread102-1432429

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
This was not my code, but borrowed from somewhere else (I do not recall, it was probably somewhere else on Tek-Tips here or delphi.about.com ).
I simply made some minor changes to suit my needs so you will probably need to do the same.

Code:
    procedure FileSearch(const PathName, FileName, origFile : string; const InDir : boolean);



procedure TfrmMyForm.FileSearch(const PathName, FileName, origFile : string; const InDir : boolean);
 var Rec  : TSearchRec;
     Path : string;
 begin
  Path := IncludeTrailingBackslash(PathName);
  if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 then  [green]// Searching just files in the given directory[/green]
   try
    [green]Execute code for when you find your file here[/green]
   finally
    FindClose(Rec);
   end;
  If not InDir then Exit;
  if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
   try
    repeat
     if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then [green]// Search through sub-directories if InDir is True[/green]
      FileSearch(Path + Rec.Name, FileName, origFile, True);
    until FindNext(Rec) <> 0;
   finally
    FindClose(Rec);
   end;
 end; [green]//procedure FileSearch[/green]

This is a recursive function to search subdirectories.
It is also currently Windows based for using the [blue]IncludeTrailingBackslash[/blue].
[blue]InDir[/blue] is used to specify if you want the procedure to check subdirectories so you can choose if it will be recursive simply based on a True / False in the procedure call.
I believe you have already included [blue]shellapi[/blue] in your uses, but if you have not, you will need to.

~
“Your request is not unlike your lower intestine: stinky, and loaded with danger.” — Ace Ventura.
 
I thank all of you for your time and the solutions. Before I got to some of these answers I modified the Find First and Find next to loop through a string list I created to check all the possible dirs (like roo suggested).

But the others solutions look simplistic too. I will use these surely and these have added to my knowledge and logic.

Thank you !!
You guys rock.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top