Solved

search for direcotry using mask

Posted on 2006-07-12
7
178 Views
Last Modified: 2010-04-05
Hi,

I need function which will search for folder using mask and return the whole
found directory brach.
For example I have directory structure on my HDD
c:\MyDir123\Mydir456\MyDir789

While searching for Mydir4* it should return c:\MyDir123\Mydir456

Maybe someone has such function?

Regards
Tom
0
Comment
Question by:Tomazaz
  • 4
  • 3
7 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17090852
you will write a function using findfirst/findnext for everything and call it recursivly for all subfolders and for each folder call again findfirst but with the mask. I'll do a small demo for you if you think it's out of your hands
0
 
LVL 28

Expert Comment

by:2266180
ID: 17090918
here is the demo. this will return the first found directory. you can modify the code to return all directories found, by removing the "exit" and adding another parameter to the function of type tstrings and add all found directories to the list.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function finddir(path:string; mask:string):string;
var
  r: TSearchRec;
begin
  if (path<>'') and (path[length(path)]<>'\') then
    path:=path+'\';
  if FindFirst(Path + '*.*', faAnyFile, r) = 0 then
  begin
    if findfirst(path+mask, fadirectory, r) = 0 then
    begin
      result:=path+r.Name;
      findclose(r);
      exit;
    end;
    repeat
        if (r.Attr and faDirectory = faDirectory) then
           result:=finddir(path+r.name, mask);
    until FindNext(r) <> 0;
    FindClose(r);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  showmessage(finddir('c:\windows','sys*'));
end;

end.
0
 

Author Comment

by:Tomazaz
ID: 17090933
<<I'll do a small demo for you if you think it's out of your hands>>

That would be nice!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Tomazaz
ID: 17091007
This function search only in directory which is set in path. How modify this function to search inside sub-categories also?
0
 
LVL 28

Expert Comment

by:2266180
ID: 17091036
sorry, there is a bug in the function. I used the same searchrec variable for both searches. my bad:

here is the corrected version that also searches in subdirectories:

function finddir(path:string; mask:string):string;
var
  r,r2: TSearchRec;
begin
  if (path<>'') and (path[length(path)]<>'\') then
    path:=path+'\';
  if FindFirst(Path + '*.*', faAnyFile, r) = 0 then
  begin
    if findfirst(path+mask, fadirectory, r2) = 0 then
    begin
      result:=path+r2.Name;
      findclose(r2);
      exit;
    end;
    repeat
        if (r.Attr and faDirectory = faDirectory) then
           result:=finddir(path+r.name, mask);
    until FindNext(r) <> 0;
    FindClose(r);
  end;
end;
0
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 17091075
well, I'm a little ashaimed for this (I don't always make such mistakes).

here is the corrected AND tested version:

function finddir(path:string; mask:string):string;
var
  r,r2: TSearchRec;
begin
  result:='';
  if (path<>'') and (path[length(path)]<>'\') then
    path:=path+'\';
  if FindFirst(Path + '*.*', faAnyFile, r) = 0 then
  begin
    if findfirst(path+mask, fadirectory, r2) = 0 then
    begin
      result:=path+r2.Name;
      findclose(r2);
      exit;
    end;
    repeat
        if (r.Attr and faDirectory = faDirectory) and (r.name<>'.') and (r.name<>'..') then
        begin
           result:=finddir(path+r.name, mask);
           if result<>'' then break;
        end;
    until FindNext(r) <> 0;
    FindClose(r);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  showmessage(finddir('c:\windows','catroot*'));
end;
0
 

Author Comment

by:Tomazaz
ID: 17091168
Excelent.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now