Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve Files and Subfolders from a Directory? 1

Status
Not open for further replies.

simmo09

Programmer
Apr 22, 2009
82
GB
Hi, i need help, im trying to select a folder, say "C:\My Folder"

now suppose this directory contains files and subfolders, how can i loop through the directory, and put the output into a hierarchy treeview?

ive looked around and think i need to use FindNext and FindPrevious, but im not so sure how.

Thanks for the Help!
 
What is it are you trying to get done by doing this? You might answer that question, and we can come up with something better. Microsoft has most of the tools that you could ever want to do tasks in API, including treeviews of files and folders like in Explorer.

Of course, if you really want the exercise of doing it yourself, the 2nd post will do nicely, except for some minor tricks in managing the TreeView

Measurement is not management.
 
Hi, thanks for the replies - i have not had time to look at gcaramia's link yet, but in regard to glen9999's post, what im trying to achieve is not anything like a filemanager, i just want to define a folder, then all files and sufolders are listed in a hierarchy view in a treeview (or even a list).

this program will be to list all the diretories of the game gta iv :)

Thanks everyone!
 
I think you will find some examples for findnext etc in the FAQ's !!

Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
what im trying to achieve is not anything like a filemanager, i just want to define a folder, then all files and sufolders are listed in a hierarchy view in a treeview (or even a list).

This really still didn't answer the question. I'm not referring to anything like a file manager. I assume you are wanting to treeview or otherwise these files for a specific purpose. What I'm asking for is that purpose.

I'm saying there are API functions that allow you to view the file/folder structure, and then allow you to select something and then return that something into the application. An open file dialog as you will find in the VCL dialogs tab is a great example of one of these. There is another one that works for selecting directories. I'm also saying you are better off trying to use one of these than to try writing something that Microsoft has already done.

Measurement is not management.
 
Thats a fair point Glen, there is no need to recreate something that already exists. I simply want to recreate the directory structure and show it in a treeview, i wont have much time until the weekend when i can have a proper look into it.

Thanks everyone for the replies :)
 
Here's the short answer:
Drop a TDirectoryListBox and a TFileListBox on a new form.
Set DirectoryListBox1.FileList:= FileListBox1;
Create DirectoryListBox1.OnChange event. Make it look like this:
Code:
procedure TForm1.DirectoryListBox1Change(Sender: TObject);
begin
  FileListBox1.Directory := DirectoryListBox1.Directory;
end;
You're done.

Roo
Delphi Rules!
 
you dont need
FileListBox1.Directory := DirectoryListBox1.Directory;
if you have set DirectoryListBox1.FileList:= FileListBox1;

Aaron
 
I got bored during some down-time and went ahead and wrote something. It's definitely in need of work, but should help for educational purposes if anything else. Of course, as was pointed out, it is much better to just use the standard APIs (as the example was given of above).

Code:
procedure TForm1.rec_list_dir(inpath: string; WinAddr1: TTreeNode);
  var
    sr: TSearchRec;
    res: integer;
    TNode: TTreeNode;
  begin
    res := FindFirst(inpath + '\*.*', faAnyFile, sr);
    while res = 0 do
      begin
        if (Sr.Name <> '.') and (Sr.Name <> '..') then
          begin
            TNode := TreeView1.Items.AddChild(WinAddr1, Sr.Name);
            if (sr.Attr and faDirectory) = faDirectory then
              rec_list_dir(inpath + '\' + sr.name, TNode);
          end;
        res := FindNext(sr);
      end;
    FindClose(sr);
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TreeView1.Items.BeginUpdate;
  TreeView1.Items.Clear;
  rec_list_dir(DriveComboBox1.Drive + ':', nil);
  TreeView1.Items.EndUpdate;
end;

Measurement is not management.
 
Actually one last thing, i decided i only want the directories to show in the tree, the files im going to put in a listview (i can manage that), what do i need to alter your code above to ignore files and just list subfolders?

Thank You :)
 
thats funny, i looked in sysutils.pas and tried faDirectory myself, it still shows files and folders :)

i only want the folders this time
 
glenn already provided the solution, you must test the attribute :

Code:
if (sr.Attr and faDirectory) = faDirectory

read the help file on FindFirst/FindNext, it will return all normal files plus the attribute you specify.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Yes the solution for that code is simply to move the part that adds to the treeview in under the if statement that tests for attribute.

More reading below, which illustrates that the 2nd parm of findfirst is effectively useless:

faq102-7116

Measurement is not management.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top