Delphi Pages Forums

Delphi Pages Forums (http://www.delphipages.com/forum/index.php)
-   General (http://www.delphipages.com/forum/forumdisplay.php?f=2)
-   -   [SOLVED] File Search (http://www.delphipages.com/forum/showthread.php?t=217543)

robert 09-24-2016 12:55 PM

File Search
 
I have 3 folders with txt files

folder1

anytext.txt
Text_files.txt
---.txt
---.txt

Folder 2

somefile.txt
---.txt
---.txt
Text_any1.txt


Folder 3

otherfile.txt
---.txt
Text_123.txt
---.txt

I have to search file which starts with Text and read it into memo, because it differs in each folder _files.txt , _any1.txt , _123.txt

lexd 09-25-2016 11:15 PM

Their is a trick in objects you can do.
A procedure or function can call its self, In other words make a search procedure in a known folder, and when you come to a sub folder
execute a its self with a new root sub folder. This can search 100 folders deep and place the names in a TStringlist for example.

torry pages
Components / Files and Drives / Search

Jasser 09-26-2016 07:47 AM

Use the below function which load certain files with certain mask into a TStringlist:

Code:

Procedure GetFileList(aFiles : TStringList; sPath : string; sMask : string = '*.*');
var
          iIndex    : Integer;
          SearchRec : TSearchRec;
begin
          if aFiles = Nil then
            aFiles := TStringList.Create;

          if sPath[Length(sPath)] <> '\' then
            sPath := sPath + '\';

          iIndex := FindFirst(sPath+sMask, faArchive, SearchRec);

          while iIndex = 0 do begin
                aFiles.Add(SearchRec.Name);
                iIndex := FindNext(SearchRec);
          end;

          FindClose(SearchRec);
end;


Example:

Code:

var
          i : Integer;

          oFiles : TStringlist;
begin
          oFiles := TStringlist.Create;
          oFiles.Clear;


          GetFileList(oFiles,'C:\folder1','Text*.txt');

          GetFileList(oFiles,'C:\folder2','Text*.txt');

          GetFileList(oFiles,'C:\folder3','Text*.txt');


          //Now show the files names.
          for i:=0 to oFiles.Count-1 do begin
              Showmessage( oFiles[i] );


              //Or you can load them into a memo.
              Memo1.Lines.LoadFromFile( oFiles[i] );
          end;

          //Finally free the object.
          oFiles.Free;


Jasser 09-26-2016 08:04 AM

If you want to include the file path with the name of the file then use the below procedure with the same previous example:


Code:

Procedure GetFileList(aFiles : TStringList; sPath : string; sMask : string = '*.*');
var
          iIndex    : Integer;
          SearchRec : TSearchRec;
begin
          if aFiles = Nil then
            aFiles := TStringList.Create;

          if sPath[Length(sPath)] <> '\' then
            sPath := sPath + '\';

          iIndex := FindFirst(sPath+sMask, faArchive, SearchRec);

          while iIndex = 0 do begin
                aFiles.Add(sPath+SearchRec.Name);
                iIndex := FindNext(SearchRec);
          end;

          FindClose(SearchRec);
end;


lexd 09-26-2016 10:46 AM

yes you have the basic idea

myseach = class(TObject)
private
FList: Stringlist;

public
create
Distroy
Procedure search(Dir: String);
function ReadFileList: Stringlist; // only read the list
end;

Create
begin
FList := TStringList.Create;
iIndex := FindFirst(sPath+sMask, faArchive, SearchRec);
end;

search
Var iIndex : Integer;
SearchRec : TSearchRec;
// data re added to stack with each subdir or recall of procedure
begin
repeat
Findnext
IF aDir then Search(SubDir); // loops into subdir and returns back hear
If FileIWant then FList.add();
Until ..........
end;

destroy
begin
FindClose(SearchRec);
FList.free;
end;

//This can search all of C drive easily
// read about objects and look at 'Classes.pas' file for examples

Jasser 09-26-2016 10:56 AM

I've being using this procedure (which I wrote myself) for more than 12 years ago. However, I can easily customize it to search the whole drive, but never needed to do that.

lexd 09-26-2016 11:00 AM

put the restrictions as you need in your code (common sence)
its fairly easy so long as you can do some OOP skills.
Or copy from Torry pages and modify it

Jasser 09-26-2016 03:24 PM

OOP is not really necessary all the times for simple tasks. Some time the performance price comes with it.


All times are GMT. The time now is 03:13 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.