Solved

search for direcotry using mask

Posted on 2006-07-12
7
180 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 137
Delphi XE10, MySQL Query 4 143
add combobox item based on numbers 9 144
Can Viruses spread while transferring Binary data with Winsock 2 77
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

809 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