?
Solved

[DELPHI] File Search

Posted on 2003-03-10
7
Medium Priority
?
777 Views
Last Modified: 2012-08-14
Hello all,

I'm trying to get my app to do a file search. It should search all drives and sub directories for a certain file(s). Some example source would be nice any help is highly appreciated. Thanx.

Sincerely, Tom Lenen
0
Comment
Question by:TomLenen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Accepted Solution

by:
ILE earned 120 total points
ID: 8107343
now not the example but the working solution

u will be call this procedure with something like

getalldir('c:\idapi\','bdecfg.*');


u need one one listbox with name s1

and the procedure is




procedure tform1.getalldir(s:string;p:string);
var rec :tsearchrec;
begin
if sysutils.findfirst(s+p,faanyfile,rec)=0 then
repeat
 s1.items.add(s+rec.name);
until sysutils.findnext(rec)<>0;
if sysutils.findfirst(s+'*.*', fadirectory, Rec)=0 then
  repeat
    if (rec.name<>'.') and (rec.name<>'..') and (rec.attr=fadirectory) then getalldir(s+rec.name+'\',p);
  until SysUtils.FindNext(Rec) <> 0;
sysutils.FindClose(rec);
end;


recursion POWERFUL A?


:))
i answer before simular question
0
 
LVL 6

Expert Comment

by:swift99
ID: 8108008
Very elegant solution.

If your drive is very large and/or your directory structure very complex then you may wish to capture the sub-directories in a list and process them after closing the main routine's find.

Because of the nature of Windows I/O, this recursive function is also susceptible to speedup by multi-threading.  Off the top of my head, I estimate that you can get at least three times the overall performance and possibly better by careful use of multi-threading.  I suggest no more than three threads per CPU.
0
 
LVL 1

Expert Comment

by:TAZI
ID: 8109675
Hi

Below is a procedure which will search through a directory looking for all file times with the extention .jpg

Hope this helps


procedure FindImages;
begin
var
  Search: TSearchRec;
  Path : String;
  Error: Integer;
  Bmp: TJPEGImage;        
  i : SmallInt;
 begin

  Error := FindFirst(Path + '*.jpg', faAnyFile - faDirectory, Search);
  if (Error = 0) then
     begin
       i := 0;
       SetLength(BmpArray,TotalImages);
       while (Error = 0) do
         begin
           Bmp := TJPEGImage.Create;
           Bmp.LoadFromFile(Path + Search.Name);
           BmpArray[i] := Bmp;
           Error := FindNext(Search);
           inc(i)
        end;
     FindClose(Search);
    end;
end;
end;

Regards
TAZI
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Assisted Solution

by:merry_prince
merry_prince earned 120 total points
ID: 8109706
1) use below function to retrieve all driver info:
procedure TForm1.btnGetDriveTypeClick(Sender: TObject);
var  I: Integer;
     C: String;
     DType: Integer;
     DriveString: String;
begin
  {loop A to Z}
  for I:=65 to 90 do begin
    //format a string to specify root catalog
    C := chr(I) + ':\';
    //invoke GetDriveType() to return a integer
    DType := GetDriveType(PChar(C));
    //format a string accroding to driver type
    case DType of
      0: DriveString := C + ' drive type cannot be determined.' ;
      1: DriveString := C + ' root directory does not exist.';
      DRIVE_REMOVABLE: DriveString := C + ' drive can be removed from the drive';
      DRIVE_FIXED: DriveString := C + ' disk cannot be removed from the drive.';
      DRIVE_REMOTE: DriveString := C + ' drive is a remote (network) drive.';
      DRIVE_CDROM: DriveString := C + ' drive is a CD-ROM drive.';
      DRIVE_RAMDISK: DriveString := C + ' drive is a RAM disk.';
    end; {case}

    if not ((DType=0) or (DType=1)) then
      lbDrive.Items.AddObject(DriveString, pointer(I));
  end;
end; {btnGetDriveTypeClick}

2) Search certain files by below func:

lbFiles: TListBox;
Private
  FFileName: String;  //File Name you want to search

procedure TfrmMain.FindFiles(APath: String);
var FSearchRec, DSearchRec: TSearchRec;
    FindResult: Integer;

  function IsDirNotation(ADirName: String): Boolean;
  begin
    Result := (ADirName = '.') or (ADirName = '..');
  end;

  function GetDirectoryName(Dir: String): String;
  begin
    if Dir[Length(Dir)] <> '\' then
      Result := Dir+'\'
    else
      Result := Dir;
  end;

begin
  APath := GetDirectoryName(APath);
  FindResult := FindFirst(APath +FFileName, faAnyFile+faHidden+faSysFile+faReadOnly, FSearchRec);
  try
    //Continue to search matchable files
    while FindResult = 0 do begin
      lbFiles.Items.Add(LowerCase(APath + FSearchRec.Name));
      FindResult := FindNext(FSearchRec);
    end;

    //Search files in subfolder of current folder
    FindResult := FindFirst(APath + '*.*', faDirectory, DSearchRec);
    while FindResult = 0 do begin
      if ((DSearchRec.Attr and faDirectory) = faDirectory) and not
        IsDirNotation(DSearchRec.Name) then
        FindFiles(APath+DSearchRec.Name);
      FindResult := FindNext(DSearchRec);
    end;
  finally
    FindClose(FSearchRec);
  end;
end; {FindFiles}
0
 

Expert Comment

by:CleanupPing
ID: 9316852
TomLenen:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 10090990
Hi!
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:

Split between ILE and merry_prince

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

...Snehanshu
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month15 days, left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question