Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 09-24-2016, 12:55 PM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 382
Default 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
  #2  
Old 09-25-2016, 11:15 PM
lexd lexd is offline
Senior Member
 
Join Date: Feb 2015
Posts: 268
Default

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
  #3  
Old 09-26-2016, 07:47 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

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;
__________________
Regards,
Abdulaziz Jasser
  #4  
Old 09-26-2016, 08:04 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

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;
__________________
Regards,
Abdulaziz Jasser
  #5  
Old 09-26-2016, 10:46 AM
lexd lexd is offline
Senior Member
 
Join Date: Feb 2015
Posts: 268
Default

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
  #6  
Old 09-26-2016, 10:56 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

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.
__________________
Regards,
Abdulaziz Jasser
  #7  
Old 09-26-2016, 11:00 AM
lexd lexd is offline
Senior Member
 
Join Date: Feb 2015
Posts: 268
Default

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
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 04:52 PM.


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