Solved

filelistbox with full path and files from subdirectory's

Posted on 2001-07-16
3
1,693 Views
Last Modified: 2010-04-06
I need a way to create a list (filelistbox?) of all filenames (full path, ex:'c:\windows\temp\readme.txt') that compare a given mask (with wildchars).  Also all the files (full path) in any subdirectory that compare that same given mask.  Example: 'c:\*.*' has to give a list off all the files on drive C with there full path.  Included all the hidden, system, ... files.  I need the lists to create correct backups.  I hope you understand what I mean (and need).

Thanks in advance.

Dirk.

PS sorry for my poor English.
0
Comment
Question by:ka1a
[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
3 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 50 total points
ID: 6287729
Try this:


procedure GetFiles(DirStr: string; filelist: TStrings);
var DirInfo: TSearchRec;
    r : Integer;
    pattern: String;
begin
  pattern := ExtractFileName(DirStr);
  if Pos('*', pattern) > 0 then
    DirStr := ExtractFilePath(DirStr)
  else
    pattern := '*.*';
  if DirStr[Length(DirStr)] <> '\' then
    DirStr := DirStr + '\';
  if SetCurrentDir((DirStr)) then
  begin
    r := FindFirst(pattern, FaAnyfile, DirInfo);
    while r = 0 do
    begin
      if (DirInfo.Attr and faDirectory) = 0 then
        filelist.Add(DirStr + DirInfo.Name);
      r := FindNext(DirInfo);
    end;
    FindClose(DirInfo);
    r := FindFirst('*.*', FaAnyfile, DirInfo);
    while r = 0 do
    begin
      if (DirInfo.Attr and faDirectory) <> 0 then
        if (DirInfo.Name <> '.') and (DirInfo.Name <> '..') then
          GetFiles(DirStr + DirInfo.Name + '\' + pattern, filelist);
      r := FindNext(DirInfo);
    end;
    FindClose(DirInfo);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.BeginUpdate;
  GetFiles('c:\*.pas', ListBox1.Items);
  ListBox1.Items.EndUpdate;
end;
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6287750
You could try something like this:

var
  Form1: TForm1;
  Count : Integer;

implementation

{$R *.DFM}

procedure GetAllFiles(S : string);
var
  Search : TSearchRec;
  FPath, FName : string;
begin
  FName := ExtractFileName(S);
  FPath := ExtractFilePath(S);

  if FindFirst(S, $23, Search) = 0 then
  begin
    repeat
      Form1.ListBox1.Items.Add(FPath + Search.Name);
      Inc(Count);
    until
      FindNext(Search) <> 0;
  end;

  if FindFirst(FPath + '*.*', faDirectory, Search) = 0 then
  begin
    repeat
      if ((Search.Attr and faDirectory) = faDirectory) and
         (Search.name[1] <> '.') then
      begin
        GetAllFiles(FPath + Search.Name + '\' + FName);
      end;
    until
      FindNext(Search) <> 0;
    FindClose(Search);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Dir, WildCard : string;
begin
  Screen.Cursor := crHourGlass;
  Count := 0;
  ListBox1.Items.Clear;
  Dir := Copy(Edit1.Text, 0, Length(Edit1.Text) - 3);
  WildCard := Copy(Edit1.Text, Length(Edit1.Text) - 2, 3);
  GetAllFiles(Dir + WildCard);
  if Count = 0 then
    ShowMessage('No Files Found.');
  Screen.Cursor := crDefault;
end;

Cheers,

Alan
0
 

Author Comment

by:ka1a
ID: 6287752
Thanks.

Your solution is excellent.  Thanks again.

Dirk.
0

Featured Post

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!

Question has a verified solution.

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

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…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 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