There is a class code of a folder and a file (since you need to distinguish them), inherited from TComponent, a code for recursively traversing a folder. How to create corresponding objects with regard to inheritance (problem in specifying inheritance), i.e. for each object, specify the owner in the Create constructor, which in the future will enable the serialization of children using the GetChildren procedure. There is an array of TComponent objects in the code, so it is necessary to memorize the parent indexes (Owner) and descendants for inheritance as needed. Code attached below
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private { Private declarations } public { Public declarations } end; Tfolder=class(TComponent) private { Private declarations } FPath : string; FName : string; FComponent : TComponent; public { Public declarations } procedure SetFName(var Name: string); function GetFName : string; procedure SetFPath(var Path: string); function GetFPath : string; protected { Protected declarations } procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override; procedure SetParentComponent (Value: TComponent); override; published { Published declarations } property Path : string read FPath write FPath; property Name : string read FName write FName; end; TFile=class(TComponent) private { Private declarations } FPath : string; FName : string; FSize : integer; public { Public declarations } procedure SetFName(var Name: string); function GetFName : string; procedure SetFPath(var Path: string); function GetFPath : string; procedure SetFSize(var Size: integer); function GetFSize : integer; protected { Protected declarations } procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override; procedure SetParentComponent (Value: TComponent); override; published { Published declarations } property Path : string read FPath write FPath; property Name : string read FName write FName; property Size : integer read FSize write Fsize; end; var Form1: TForm1; ArrTfolder[200] : Tfolder;//массивы создаваемых объектов ArrTfile[200] : Tfile; implementation {$R *.dfm} procedure Tfolder.SetFName(var Name : string); begin FName:=Name; end; function Tfolder.GetFName : string; begin Result:=FName; end; procedure Tfolder.SetFPath(var Path : string); begin FPath:=Path; end; function Tfolder.GetFPath : string; begin Result:=FPath; end; procedure TFile.SetFName(var Name : string); begin FName:=Name; end; function TFile.GetFName : string; begin Result:=FName; end; procedure TFile.SetFPath(var Path : string); begin FPath:=Path; end; function TFile.GetFPath : string; begin Result:=FPath; end; procedure TFile.SetFSize(var Size : integer); begin FSize:=Size; end; function TFile.GetFSize : integer; begin Result:=FSize; end; procedure Tfolder.GetChildren(Proc: TGetChildProc; Root: TComponent); var I: integer; OwnedComponent : TComponent; begin inherited GetChildren(Proc, Root); if Root = Self then for I := 0 to ComponentCount - 1 do begin OwnedComponent := Components[I]; if not OwnedComponent.HasParent then Proc(OwnedComponent); end; end; procedure Tfolder.SetParentComponent (Value: TComponent); begin end; procedure TFile.GetChildren(Proc: TGetChildProc; Root: TComponent); var I: integer; OwnedComponent : TComponent; begin inherited GetChildren(Proc, Root); if Root = Self then for I := 0 to ComponentCount - 1 do begin OwnedComponent := Components[I]; if not OwnedComponent.HasParent then Proc(OwnedComponent); end; end; procedure TFile.SetParentComponent (Value: TComponent); begin end; procedure FindFiles(const DirPath: string; Str: TStrings); var SR: TSearchRec; begin if FindFirst(DirPath + '\*.*', faAnyFile, SR) = 0 then try repeat if not ((SR.Name = '.') or (SR.Name = '..')) then begin if SR.Attr = faDirectory then FindFiles(DirPath + '\' + SR.Name, Str) else Str.Add(SR.Name); end; until FindNext(SR) <> 0; finally FindClose(SR); end; end; end.