Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

[DELPHI] File Search

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
TomLenen
Asked:
TomLenen
2 Solutions
 
ILECommented:
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
 
swift99Commented:
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
 
TAZICommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
merry_princeCommented:
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
 
CleanupPingCommented:
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
 
snehanshuCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now