Solved

filelistbox with full path and files from subdirectory's

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

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

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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