How to list files and subdirectories in Delphi?

Asked

Viewed 2,378 times

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 tree, I found some examples on the WEB, but none was useful enough, as it only listed superficial directories and the subsequents were not listed.

A very clear example of how it should be is the image below:inserir a descrição da imagem aqui

  • Are you developing a console application? Need it designed or in text form?

  • No, actually this image was just to give example, actually the application is VLC

3 answers

2


Since you did not specify what type of component should be displayed, in the example below this is done using a Ttreeview. You will need a Ttreeview and a button in the form.

Source: http://www.swissdelphicenter.ch/torry/showcode.php?id=400

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.

  • Congratulations, best answer, I was able to save the data in a text file in the same way as a Tmemo.

1

I have a procedure I believe will solve 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 (component of the Win32 tab)

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 Voce uses:

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

where

mmoSaida: TMemo;

inserir a descrição da imagem aqui

  • His example was a simple copy of another that I saw on the internet earlier and as I said, it does not list sub-directories in tree, still I put an image in the pegunta to help in the reader’s understanding. In addition, your example references a variable that was not created "Strings" (6th line). Forgive me if I’m wrong, so I ask you to make your answer as clear and if possible to take a test before answering/correcting.

  • I changed the code there, only you put the strPrefix you want.

  • I tried every possible way, but there are always flaws, you could "upair" your Unity on a cloud and share the link here?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.