How to list files and sub-directories in Delphi?

5

I created a project in Pascal-Object and at a given time it should list all the files and subdirectories located in the Desktop in tree, I found some examples in the WEB, but none was useful enough, because it only listd superficial directories and the sub -sequences were left unlisted.

A very clear example of how it should look is the image below:

    
asked by anonymous 08.02.2016 / 17:21

3 answers

1

Since you did not specify what type of component to display, the example below is done using a TTreeView. You will need a TTreeView and a button on the form.

Source: link

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure GetDirectories(Tree: TTreeView; Directory: string; Item: TTreeNode;
      IncludeFiles: Boolean);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Node: TTreeNode;
  Path: string;
  Dir: string;
begin
  Dir := 'c:\temp\';
  Screen.Cursor := crHourGlass;
  TreeView1.Items.BeginUpdate;
  try
    TreeView1.Items.Clear;
    GetDirectories(TreeView1, Dir, nil, True);
  finally
    Screen.Cursor := crDefault;
    TreeView1.Items.EndUpdate;
  end;
end;

procedure TForm1.GetDirectories(Tree: TTreeView; Directory: string; Item: TTreeNode; IncludeFiles: Boolean);
var
  SearchRec: TSearchRec;
  ItemTemp: TTreeNode;
begin
  Tree.Items.BeginUpdate;
  if Directory[Length(Directory)] <> '\' then Directory := Directory + '\';
  if FindFirst(Directory + '*.*', faDirectory, SearchRec) = 0 then
  begin
    repeat
      if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1] <> '.') then
      begin
        if (SearchRec.Attr and faDirectory > 0) then
          Item := Tree.Items.AddChild(Item, SearchRec.Name);
        ItemTemp := Item.Parent;
        GetDirectories(Tree, Directory + SearchRec.Name, Item, IncludeFiles);
        Item := ItemTemp;
      end
      else if IncludeFiles then
        if SearchRec.Name[1] <> '.' then
          Tree.Items.AddChild(Item, SearchRec.Name);
    until FindNext(SearchRec) <> 0;
    FindClose(SearchRec);
  end;
  Tree.Items.EndUpdate;
end;
end.

But there are other components that can be used in this sense.

    
15.02.2016 / 17:29
1

I have a procedure that I believe solves your problem:

procedure frmTeste.BuscaSubDiretorios(strNomeDir: String; Pai: TTreeNode);
var
  sr: TSearchRec;
  FileAttrs: Integer;
  strNome: String;
begin
  FileAttrs := faDirectory;
  strNome := strNomeDir;
  while strNome[Length(strNome)] = '\' do
    strNome := copy(strNome,1,length(strNome) - 1);
  if strNome[Length(strNome)] <> ':' then
    strNome := ExtractFileName(strNome)
  else
    strNome := copy(strNome,1,length(strNome) - 1);
  Pai := TreeView1.Items.AddChild(Pai,strNome);

  if FindFirst(strNomeDir + '*.*', FileAttrs, sr) = 0 then
  begin
    begin
      repeat
        //verifica se o arquivo encontrado é uma pasta
        if (sr.Name <> '.') and (sr.Name <> '..') and
           ((sr.Attr > 15) and (sr.Attr < 32)) then 
        begin
          BuscaSubDiretorios(strNomeDir + sr.Name + '\',Pai);
        end;
      until FindNext(sr) <> 0;
      FindClose(sr);
    end;
  end;
end;

To call the procedure you use: BuscaSubDiretorios('c:\temp\',TreeView1.Selected);

Note that we are using a TreeView (Win32 tab component)

    
10.02.2016 / 11:27
0
procedure TForm1.Listar(const Strings: TStrings; const strDiretorio: string; strPrefixo: string);
var
   sr: TSearchRec;
begin
   if FindFirst(strDiretorio + '\*', faDirectory, sr) = 0 then
      try
         Strings.BeginUpdate;
         try
            repeat
               if (sr.Name <> '.') and (sr.Name <> '..') then
               begin
                  if sr.Attr = faDirectory then
                  begin
                     Strings.Add(strPrefixo + strDiretorio + '\' + sr.Name);
                     Listar(Strings, strDiretorio + '\' + sr.Name, strPrefixo + #9);
                  end;

               end;
            until FindNext(sr) <> 0;
         finally
            Strings.EndUpdate;
         end;
      finally
         FindClose(sr);
      end;
end;

Ai you use:

Listar(mmoSaida.Lines, 'D:\Diretorio');

where

mmoSaida: TMemo;

    
08.02.2016 / 19:34