Solved

search for direcotry using mask

Posted on 2006-07-12
7
177 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:ciuly
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:ciuly
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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:ciuly
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:
ciuly 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

20 Experts available now in Live!

Get 1:1 Help Now