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. 
  • 2
    > It is necessary to carry out the creation. What is the question? - Nofate
  • one
    Something through the Alps. First, the two classes are not needed, especially since they have the same methods and properties. One TFile is enough. Secondly, you need to build an algorithm for filling (creating components by file structure), an input point is needed. While it is not. Thirdly, parent (or Owner - what is needed there, the task is not very clear) to fill in during the directory traversal. Those. create a child and immediately assign parent to it. Formulate the task in detail what you will do with these components and why you need TComponent. - Yura Ivanov
  • question has been edited - ivan89
  • As I understand it, you need to add ArrTfolder [0] .Create (nil); ArrTfolder [0] .SetFPath ("D: \ Root"); // allow the path to the root directory D: \ Root ArrTfolder [0] .SetFName ("Root"); This is about the entry point (commentary to (1 hour ago) Yura Ivanov), but what about inheritance and indices of inherited elements? - ivan89

1 answer 1

Reduce redundant entities:

 TFile=class(TComponent) private FPath : String; FSize : Int64; FParent: TFile; published property Parent: TFile read FParent write FParent; property Path : String read FPath write FPath; property Size : Int64 read FSize write Fsize; end; procedure FindFiles(const DirPath: string; parent: TFile; Owner: TComponent); var SR: TSearchRec; mfile: TFile; begin if FindFirst(DirPath + '\*.*', faAnyFile, SR) = 0 then try repeat if not ((SR.Name = '.') or (SR.Name = '..')) then begin mfile:=TFile.Create(Owner); mfile.Path:=DirPath+ '\' + SR.Name; mfile.Size:=SR.Size; mfile.Parent:=parent;//Это новое свойство в TFile if SR.Attr = faDirectory then FindFiles(DirPath + '\' + SR.Name, mfile, Owner); end; until FindNext(SR) <> 0; finally FindClose(SR); end; end; procedure TForm1.MagicButtonClick(Sender: TObject); begin FindFiles('D:\',nil,Self); end; 

So, files turned into components. Why do you need it?