Recent

Author Topic: Recursive search for a specific file  (Read 2416 times)

BobDog

  • New Member
  • *
  • Posts: 40
Re: Recursive search for a specific file
« Reply #15 on: March 10, 2019, 02:44:50 am »

Thanks.
I dont have lazarus installed.
It contains a old gcc.exe for resource files I believe, I have MinGW on path.
I know I could knock lazarus off path once installed I suppose.
Same with Linux, I have the gcc development package in situ, so I give freepascal a miss there.
However that's all to sort out another day.

For all units, I copy and  pop them in a desktop folder when required and include the path my ide.

 

lucamar

  • Hero Member
  • *****
  • Posts: 2145
Re: Recursive search for a specific file
« Reply #16 on: March 10, 2019, 03:06:06 am »
I dont have lazarus installed.
It contains a old gcc.exe for resource files I believe, I have MinGW on path.
I know I could knock lazarus off path once installed I suppose.
Same with Linux, I have the gcc development package in situ, so I give freepascal a miss there.
However that's all to sort out another day.

For all units, I copy and  pop them in a desktop folder when required and include the path my ide.

Huh? Lazarus does not include GCC; it includes (in Windows) the binary utils: as, ld, etc. and GDB. In Linux and other *nixen it simply uses the sytem's ones if they are (and if not it fails).

I've never actually seen a Lazarus mangle a MinGW intallation or a Linux GCC instalation.

Anyway, one thing you could do is download the Lazarus "source" package and unpack it somewhere. In fact, that's the default install: a directory containing the binaries, compiled units and packages and sources.

I would not do it that way but I'm not you, obviously ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 612
Re: Recursive search for a specific file
« Reply #17 on: March 10, 2019, 03:19:05 am »
@lucamar

I copied down the program you pointed out in your reply #1.

I don't think it works right. Always returns the same file.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 612
Re: Recursive search for a specific file
« Reply #18 on: March 10, 2019, 03:41:04 am »
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   FindAllAptDotDat;
  4. end;
  5.  
  6. procedure TForm1.FindAllAptDotDat;
  7.  Var
  8.  DirName : String = 'C:\X-Plane 11\Custom Scenery\';
  9.  FileName : String = 'apt.dat';
  10.  IDir : Boolean = True;
  11.   begin
  12.     FileSearch(DirName,FileName, Idir);
  13.   end;
  14.  
  15. procedure TForm1.FileSearch(const PathName, FileName : string; const InDir : boolean);
  16. var Rec  : TSearchRec;
  17.     Path : string;
  18. begin
  19. Path := IncludeTrailingBackslash(PathName);
  20. if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 then
  21.  try
  22.    repeat
  23.      Label2.Caption:= Path + Rec.Name;
  24.      Label2.refresh;
  25.      ListBox1.Items.Append(Path + Rec.Name) ;
  26.      ListBox1.MakeCurrentVisible ;
  27.  
  28.    until FindNext(Rec) <> 0;
  29.  finally
  30.    FindClose(Rec);
  31.  end;
  32.  
  33. If not InDir then Exit;
  34.  
  35. if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
  36.  try
  37.    repeat
  38.     if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then
  39.      FileSearch(Path + Rec.Name, FileName, True);
  40.    until FindNext(Rec) <> 0;
  41.  finally
  42.    FindClose(Rec);
  43.  end;
  44. end; //procedure FileSearch
  45.                                
Found this and it seems to work.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 2166
Re: Recursive search for a specific file
« Reply #19 on: March 10, 2019, 02:15:26 pm »
but that is assuming a fixed folder location..

what if user wants to in stall it elsewhere?

you need to learn how to use User space folders  :D
Number 1 at blue screen app creations!

lucamar

  • Hero Member
  • *****
  • Posts: 2145
Re: Recursive search for a specific file
« Reply #20 on: March 10, 2019, 02:33:29 pm »
@lucamar

I copied down the program you pointed out in your reply #1.

I don't think it works right. Always returns the same file.

That is by design of that specific program. I told you how to modify it in this other post.

But if that other solution you found works, that's OK. It could be made more compact and more platform-aware (p.e. using AllFilesMask instead of '*.*') but it's not bad. :)

Some hours later

OK, it being Sunday and I being bored, I've spent a little time writing a little control for you. It's a simple TListBox descendant with some added properties: Directory, FileMask, Recurse, etc.

To search for a file/files in a directory you just have to do:
Code: [Select]
  FindFilesBox.Active := False;
  FindFilesBox.Directory := MyDirectory;
  FindFilesBox.FileMask := MyFileName;
  FindFilesBox.Active := True;
or, alternatively, use the handy built-in method:
Code: [Select]
FindFilesBox.FindFilesIn(MyDirectory, MyFilename, Recursing);
Since it's not (yet) in a package it can't be installed in the IDE: you need to instance it at run-time. I've added a small demo project to the attached zip to show how it can be used.

Have fun! :D
« Last Edit: March 10, 2019, 08:04:51 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 612
Re: Recursive search for a specific file
« Reply #21 on: March 20, 2019, 04:52:45 am »
@Lucmar
"FindFilesBox.FindFilesIn(MyDirectory, MyFilename, Recursing);"


Yea, I Like that Demo program.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

TomTom

  • Full Member
  • ***
  • Posts: 114
Re: Recursive search for a specific file
« Reply #22 on: March 22, 2019, 01:40:00 pm »
I'm using cyFileSearch from Cindy package (https://packages.lazarus-ide.org/ just type Cindy in search). It's very easy to use and it's convinient :)