?
Solved

search for direcotry using mask

Posted on 2006-07-12
7
Medium Priority
?
188 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
[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
  • 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
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!

 

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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 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