Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

filelistbox with full path and files from subdirectory's

Posted on 2001-07-16
3
Medium Priority
?
1,820 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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

886 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