The following unit demonstrates scanning a folder for directories and subdirectories and storing the results in a binary tree structure. http://www.awitness.org/delphi_pascal_tutorial/index.html unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, FileCtrl; type link=^node; node=record filename:string; l, r:link end; {tree node to store directories} TForm1 = class(TForm) FileSearch: TButton; TreePrint: TButton; procedure FormCreate(Sender: TObject); procedure FileSearchClick(Sender: TObject); function FileLook(Filespec:string):boolean; Function TreeInsert(Fname:string; q:link):link; procedure TreePrintClick(Sender: TObject); Procedure PrintTree(q:link); private { Private declarations } public { Public declarations } end; var Form1: TForm1; t, head, z: link; fs:textfile; {*********************************} flcount:integer; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin new(z); new(head); {initialize binary tree head} head^.filename:=''; head^.r:=z; head^.l:=z; {set key to nothing, lowest value on sorted tree} {z is a dummy node, similar to setting to 'nil'} flcount:=0; {count the number of folders during the print routine} end; procedure TForm1.FileSearchClick(Sender: TObject); var found:boolean; begin Found:=FileLook('c:\windows\*.*'); {**************scan this folder for sub dirs} If found then MessageDlg('DUN!', mtInformation, [mbOk], 0) else MessageDlg('Directory does not exist', mtError, [mbOk], 0); end; Function TForm1.FileLook(Filespec:string):boolean; {scan for subdirs code} var x:link; {tree link, only needed if you had additional fields in the record to fill} validres:integer; {SearchRec routines return 0 if successful} SearchRec : TSearchRec; DirPath, DirName, Flname : string; {search for subdirs flname will be *.*} begin DirPath:=ExtractFilePath(FileSpec); Result:= DirectoryExists(DirPath); If not Result then exit; {some error checking code} Flname:=ExtractFileName(FileSpec); {save the wild cards in the filespec} validres := FindFirst(FileSpec, faDirectory, SearchRec); while validres=0 do begin If (SearchRec.Attr and faDirectory>0) and (SearchRec.Name[1]<>'.') then begin {ignore the two dotted directories, do an and on the attributes to confirm that the current searchrec is a directory} dirname:=SearchRec.Name; {extract the directory name} x:=TreeInsert(dirpath + dirname, head); {add the dirname to the current path and insert onto tree} FileLook(Dirpath+ dirname + '\' + Flname); {now check this new dir for subdirs using a recursive call to the function} end; validres:=FindNext(SearchRec); {if it was not a directory or function returned from recursive search for subdirectories keep looking in current path} end; end; Function TForm1.TreeInsert(Fname:string; q:link):link; var st:link; {st - the previous link, search until insertion value is less than current tree val} begin repeat st:=q; if fnamez then begin PrintTree(q^.l); Writeln(fs, inttostr(flcount) + ' : ' + q^.filename); flcount:=flcount+1; {compare count to property menu directory count} PrintTree(q^.r); end; end; end.