The source code below scans for directories and subdirectories from a given starting point and stores all the paths in a binary tree structure. You will need to drop two buttons onto a form, name one Filesearch, and the other tree print. Then use selectall and delete to remove the code for the unit and replace it with the code below to run the program. After you have pasted the replacement code into the unit, remember to click on the form and then choose the events tab on the properties list, and then double click on 'form create' and also remember to double click on the 'onclick' events for both buttons to get the code to run. You can also modify the code to scan for directories in whatever folder you wish (the AssignFile statement defaults to 'c:\windows\*.*' for the directory scan and outputs to 'c:\treeprint.txt'). You will notice a variable named Flcount which is incremented during the TreePrint routine. You can right click on the folder you choose to scan and then make a note of how many directory and subdirectory folders are in the chosen starting folder by choosing properties from the pop up menu (the treeprint routine prints out one 'blank' which represents the empty root of the tree, so the variable is initialized to zero).
You can download the code as a textfile by right clicking on the following link ->directory_search_recursive.txt
A related page scans subdirectories and puts them onto a Delphi TreeView.
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}
& nbsp; &nb sp; {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
& nbsp; {ignore the two dotted directories, do an and on the attributes to confirm
& nbsp; that the current searchrec is a directory}
dir name:=SearchRec.Name; {extract the directory name}
x: =TreeInsert(dirpath + dirname, head); {add the dirname to the current path
& nbsp; &nb sp; & nbsp; and insert onto tree}
Fil eLook(Dirpath+ dirname + '\' + Flname); {now check this new dir for subdirs
& nbsp; &nb sp; & nbsp; using a recursive call to the function}
end;
validres:=FindNext(SearchRec) ; {if it was not a directory or function returned
& nbsp; &nb sp; from recursive search for subdirectories keep looking
& nbsp; &nb sp; 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 fname<q^.filename then q:=q^.l else q:=q^.r;
until q=z; {dummy node indicates search end, similar to nil pointer, easier to code}
new(q); q^.filename:=fname; {create a new leaf node, insert value}
q^.l:=z; q^.r:=z; {set leaf node pointers to dummy value}
if fname<st^.filename then st^.l:=q else st^.r:=q; {use st to attach leaf to tree}
Result:=q; {result only needed if you had additional record fields to fill in}
end;
procedure TForm1.TreePrintClick(Sender: TObject);
begin
assignfile(fs, 'c:\treeprint.txt'); {******************}
rewrite(fs);
PrintTree(head); {pass the head pointer of the tree}
CloseFile(fs);
MessageDlg('TREE DUN!', mtInformation, [mbOk], 0);
end;
Procedure Tform1.PrintTree(q:link);
{recursively search tree to left (less than) nodes and then search to the right (greater than nodes) to get an alphabetically sorted printout to file}
begin
if q<>z 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.
A Unified Field Theory
![]()
The Unified Field Theory
is also available as a zip file -> unified.zip
Introduction :The Pioneer Effect and the New Physics. A brief description of the new physics required to explain the 'Pioneer Effect', which is the constant deceleration of space craft as they fly through space.

