如何得到一个文件夹下面的所有的子文件夹的列表以及子文件加的大小 ( 积分: 45 )

  • 主题发起人 主题发起人 jljzjybust
  • 开始时间 开始时间
J

jljzjybust

Unregistered / Unconfirmed
GUEST, unregistred user!
我所遇到的问题就是想要得到一个文件夹下面的子文件夹的名字和大小,从来没有做过关于这方面的东西,请教各位高手最好能给出一个例子谢谢
 
我所遇到的问题就是想要得到一个文件夹下面的子文件夹的名字和大小,从来没有做过关于这方面的东西,请教各位高手最好能给出一个例子谢谢
 
才45分也太少了,加分就告诉你,给个实例给你,呵呵
 
就这么些分了,没有了要是有的话肯定给你加
 
帮帮忙了
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls, Buttons, ExtCtrls, FileCtrl;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Panel1: TPanel;<br> &nbsp; &nbsp;Panel2: TPanel;<br> &nbsp; &nbsp;BitBtn1: TBitBtn;<br> &nbsp; &nbsp;Panel3: TPanel;<br> &nbsp; &nbsp;Panel4: TPanel;<br> &nbsp; &nbsp;DriveComboBox1: TDriveComboBox;<br> &nbsp; &nbsp;Panel5: TPanel;<br> &nbsp; &nbsp;DirectoryListBox1: TDirectoryListBox;<br> &nbsp; &nbsp;procedure MakeTree;<br> &nbsp; &nbsp;procedure BitBtn1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure FormResize(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;TreeCount, FilesCount, DirsCount : integer;<br> &nbsp;TreeSize, FilesSize, DirsSize : comp;<br>implementation<br><br>uses Unit2;<br><br>{$R *.DFM}<br><br>procedure TForm1.MakeTree;<br>var &nbsp;Sr : TSearchRec;<br> &nbsp; &nbsp; Err : integer;<br> &nbsp; &nbsp; TrSize, FilePath : string;<br>Begin<br> Err:=FindFirst('*.*',$37,Sr) ;<br> While (Err = 0) do<br> &nbsp;begin<br> &nbsp; if Sr.Name[1]&lt;&gt;'.' then<br> &nbsp; begin<br> &nbsp; &nbsp; FilePath:=ExpandFileName(Sr.Name);<br> &nbsp; &nbsp; TreeSize:=TreeSize+Sr.Size;<br> &nbsp; &nbsp; TrSize:=FloatToStr(TreeSize);<br> &nbsp; &nbsp; Form1.Caption:=DirectoryListBox1.Directory+' &nbsp;'+IntToStr(TreeCount)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +' &nbsp;files and folders &nbsp; &nbsp;Size: '+TrSize;<br> &nbsp; &nbsp; if (Sr.Attr and faDirectory)=0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FilesSize:=FilesSize+Sr.Size;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inc(FilesCount);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; inc(TreeCount);<br> &nbsp; end;<br><br> &nbsp; { Begin Recursion }<br> &nbsp; If ((Sr.Attr and faDirectory)&lt;&gt;0)AND(Sr.Name[1] &lt;&gt; '.') then<br> &nbsp; begin<br> &nbsp; &nbsp; DirsSize:=DirsSize+Sr.Size;<br> &nbsp; &nbsp; inc(DirsCount);<br> &nbsp; &nbsp; ChDir(Sr.Name) ;<br> &nbsp; &nbsp; MakeTree ;<br> &nbsp; &nbsp; ChDir('..') ;<br> &nbsp; end ;<br> &nbsp; { End Recursion }<br> &nbsp; Err:=FindNext(Sr) ;<br> &nbsp;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> &nbsp; &nbsp;BitBtn1.Left:=round((Form1.Width-BitBtn1.Width)/2)-5;<br> &nbsp; &nbsp;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> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls, Buttons, ExtCtrls, Unit1;<br><br>type<br> &nbsp;TForm2 = class(TForm)<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;procedure BitBtn1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure FormShow(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form2: TForm2;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm2.BitBtn1Click(Sender: TObject);<br>begin<br> &nbsp; &nbsp;Close;<br>end;<br><br>procedure TForm2.FormShow(Sender: TObject);<br>begin<br> &nbsp; &nbsp; Memo1.Lines.Clear;<br> &nbsp; &nbsp; Memo1.Lines.Add(FloatToStr(DirsCount)+' folders ');<br> &nbsp; &nbsp; Memo1.Lines.Add(FloatToStr(FilesCount)+' files '<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+FloatToStr(FilesSize)+' bytes');<br> &nbsp; &nbsp; Memo1.Lines.Add(FloatToStrF(FilesSize/1024, ffNumber, 12, 2)+' kilobytes');<br> &nbsp; &nbsp; Memo1.Lines.Add('In tree '+Form1.DirectoryListBox1.Directory);<br>end;<br><br>end.<br><br><br>核心过程是MakeTree,带递归的,变量名已经起的很清楚了,看下就OK了
 
查找子文件夹:<br>FindFirst<br>FindNext<br>FindClose<br>用递归,Delphi帮助里有例子,属性里要加上faDirectory,就可以查询文件夹<br>计算文件夹大小应该只能在递归中自己算了,把每个文件的大小加起来
 
接受答案了.
 
后退
顶部