Solved

filelistbox with full path and files from subdirectory's

Posted on 2001-07-16
3
1,502 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

19 Experts available now in Live!

Get 1:1 Help Now