unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, Buttons, ExtCtrls, FileCtrl;<br><br>type<br> TForm1 = class(TForm)<br> Panel1: TPanel;<br> Panel2: TPanel;<br> BitBtn1: TBitBtn;<br> Panel3: TPanel;<br> Panel4: TPanel;<br> DriveComboBox1: TDriveComboBox;<br> Panel5: TPanel;<br> DirectoryListBox1: TDirectoryListBox;<br> procedure MakeTree;<br> procedure BitBtn1Click(Sender: TObject);<br> procedure FormResize(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> TreeCount, FilesCount, DirsCount : integer;<br> TreeSize, FilesSize, DirsSize : comp;<br>implementation<br><br>uses Unit2;<br><br>{$R *.DFM}<br><br>procedure TForm1.MakeTree;<br>var Sr : TSearchRec;<br> Err : integer;<br> TrSize, FilePath : string;<br>Begin<br> Err:=FindFirst('*.*',$37,Sr) ;<br> While (Err = 0) do<br> begin<br> if Sr.Name[1]<>'.' then<br> begin<br> FilePath:=ExpandFileName(Sr.Name);<br> TreeSize:=TreeSize+Sr.Size;<br> TrSize:=FloatToStr(TreeSize);<br> Form1.Caption:=DirectoryListBox1.Directory+' '+IntToStr(TreeCount)<br> +' files and folders Size: '+TrSize;<br> if (Sr.Attr and faDirectory)=0 then<br> begin<br> FilesSize:=FilesSize+Sr.Size;<br> inc(FilesCount);<br> end;<br> inc(TreeCount);<br> end;<br><br> { Begin Recursion }<br> If ((Sr.Attr and faDirectory)<>0)AND(Sr.Name[1] <> '.') then<br> begin<br> DirsSize:=DirsSize+Sr.Size;<br> inc(DirsCount);<br> ChDir(Sr.Name) ;<br> MakeTree ;<br> ChDir('..') ;<br> end ;<br> { End Recursion }<br> Err:=FindNext(Sr) ;<br> end ;<br>End;<br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>begin<br> TreeCount:=1;<br> FilesCount:=0;<br> DirsCount:=0;<br> TreeSize:=0;<br> FilesSize:=0;<br> DirsSize:=0;<br> ChDir(DirectoryListBox1.Directory);<br> MakeTree;<br> Form2.ShowModal;<br>end;<br><br><br>procedure TForm1.FormResize(Sender: TObject);<br>begin<br> BitBtn1.Left:=round((Form1.Width-BitBtn1.Width)/2)-5;<br> DriveComboBox1.Left:=round((Form1.Width-DriveComboBox1.Width)/2-5);<br>end;<br><br>end.<br><br><br><br><br><br>unit Unit2;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, Buttons, ExtCtrls, Unit1;<br><br>type<br> TForm2 = class(TForm)<br> Memo1: TMemo;<br> procedure BitBtn1Click(Sender: TObject);<br> procedure FormShow(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form2: TForm2;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm2.BitBtn1Click(Sender: TObject);<br>begin<br> Close;<br>end;<br><br>procedure TForm2.FormShow(Sender: TObject);<br>begin<br> Memo1.Lines.Clear;<br> Memo1.Lines.Add(FloatToStr(DirsCount)+' folders ');<br> Memo1.Lines.Add(FloatToStr(FilesCount)+' files '<br> +FloatToStr(FilesSize)+' bytes');<br> Memo1.Lines.Add(FloatToStrF(FilesSize/1024, ffNumber, 12, 2)+' kilobytes');<br> Memo1.Lines.Add('In tree '+Form1.DirectoryListBox1.Directory);<br>end;<br><br>end.<br><br><br>核心过程是MakeTree,带递归的,变量名已经起的很清楚了,看下就OK了