下面这个函数用递归的方式,遍历指定目录下的所有文件,
计算出所有文件总的字节长度
写得比较粗糙,不过基本原理就是这样了function GetPathSize(pathname: string): int64;
var
  FindData: TWin32FindData;
  hf:THandle;
  tsize:int64;
  b:boolean;
  i:integer;
  tmpstr:string;
begin
        hf := Windows.FindFirstFile(PChar(pathname + '\*.*'), FindData);
        if hf = INVALID_HANDLE_VALUE then
        begin
                Result := 0;
                exit;
        end;
        b := true;
        tsize := 0;
        i:=0;
        while b do
        begin
                if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
                begin
                tsize := tsize + FindData.nFileSizeHigh * MAXDWORD + FindData.nFileSizeLow;
                inc(i);
                end
                else
                begin
                        tmpstr := FindData.cFileName + '';
                        if (tmpstr <> '.') and (tmpstr <> '..') then
                        begin
                        tsize := tsize + GetPathSize(pathname + '\' + FindData.cFileName);
                        end;
                end;
                b := windows.FindNextFile(hf,FindData);
        end;
        Result := tsize;end;

解决方案 »

  1.   

    哈哈哈哈,刚刚下午才看到这样一段代码,来自一个报表控件的示例程序
    cbDrives下拉驱动器TreeView文件列表框
    //列驱动器
      procedure BuildDriverList;
      var
        I: Integer;
        Drive: PChar;
      begin
        for I := 0 to 31 do
        begin
          if Boolean(GetLogicalDrives and (1 SHL I)) then
          begin
            Drive := PChar(CHR(65 + I) + ':\');
            cbDrives.Items.Add(Drive);
            if I = 2 then cbDrives.ItemIndex := cbDrives.Items.Count - 1;
          end;
        end;
      end;
    procedure BuildTree(APath: string; AItem: TTreeNode);
    var
      SearchRec, Dummy: TSearchRec;
      Found: Integer;
      Node: TTreeNode;
    begin
      Found := FindFirst(APath + '*.*', faAnyFile, SearchRec);
      try
        while Found = 0 do
        begin
          if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
          begin
            if AItem <> nil then
              Node := TreeView.Items.AddChild(AItem, SearchRec.Name)
            else
              Node := TreeView.Items.Add(nil, SearchRec.Name);
            Node.StateIndex := 8;
            if SearchRec.Attr and faDirectory <> 0 then
            begin
              Node.HasChildren := FindFirst(APath + SearchRec.Name + '\*.*', faAnyFile, Dummy) = 0;
              Node.StateIndex := 6;
            end;
          end;
          Found := FindNext(SearchRec);
        end;
      finally
        FindClose(SearchRec);
      end;
    end;procedure TreeViewExpanding(Sender: TObject; Node: TTreeNode; var AllowExpansion: Boolean);  function GetNodeFullPath(Node: TTreeNode): String;
      begin
        Result := '';
        while Node <> nil do
        begin
          if Node.HasChildren then
            Result := '\' + Result;
          Result := Node.Text + Result;
          Node := Node.Parent;
        end;
        Result := cbDrives.Text + Result;
      end;begin
      if Node.HasChildren and (Node.Count = 0) then
        BuildTree(GetNodeFullPath(Node), Node);
    end;procedure TMainForm.TreeViewExpanded(Sender: TObject; Node: TTreeNode);
    begin
      Node.StateIndex := 7;
    end;procedure TMainForm.TreeViewCollapsed(Sender: TObject; Node: TTreeNode);
    begin
      Node.StateIndex := 6;
    end;
    后面两个函数意义说不上来,你可能稍加整理。前面说过今天下午发找到的,所以我还没整理呢
      

  2.   

    unit FilesManage;interfaceuses SysUtils, FileCtrl, Classes;  function SearchFiles(const SearchPath: String; const FileName: String; List: TStrings): Boolean;implementationfunction IsDirectory(const Name: String): Boolean;
    begin
      Result := DirectoryExists(Name);
    end;function IsDot(const Name: String): Boolean;
    begin
      Result := (Name = '.')or(Name = '..');
    end;function SearchFiles(const SearchPath: String; const FileName: String; List: TStrings): Boolean;
    var
      Attr: Integer;
      F: TSearchRec;
    begin
      Attr := faAnyFile;
      if FindFirst(SearchPath + '\' + FileName, Attr, F) = 0 then
      begin
        if not IsDot(F.Name) then
          List.Add(SearchPath + '\' + F.Name);    while FindNext(F) = 0 do
        begin
          if not IsDot(F.Name) then
            List.Add(SearchPath + '\' + F.Name);
          if not IsDot(F.Name)and IsDirectory(SearchPath +'\'+ F.Name) then
          begin
            SearchFiles(SearchPath +'\'+ F.Name, FileName, List)
          end;    end;
        FindClose(F);
      end;
      Result := True;
    end;end.相关连接:http://expert.csdn.net/expert/topic/866/866683.xml
    Demo: SearchFiles('D:\', '*.*', Memo1.Lines);
      

  3.   

    unit FilesManage;interfaceuses SysUtils, FileCtrl, Classes;  function SearchFiles(const SearchPath: String; const FileName: String; List: TStrings): Boolean;implementationfunction IsDirectory(const Name: String): Boolean;
    begin
      Result := DirectoryExists(Name);
    end;function IsDot(const Name: String): Boolean;
    begin
      Result := (Name = '.')or(Name = '..');
    end;function SearchFiles(const SearchPath: String; const FileName: String; List: TStrings): Boolean;
    var
      Attr: Integer;
      F: TSearchRec;
    begin
      Attr := faAnyFile;
      if FindFirst(SearchPath + '\' + FileName, Attr, F) = 0 then
      begin
        if not IsDot(F.Name) then
          List.Add(SearchPath + '\' + F.Name);    while FindNext(F) = 0 do
        begin
          if not IsDot(F.Name) then
            List.Add(SearchPath + '\' + F.Name);
          if not IsDot(F.Name)and IsDirectory(SearchPath +'\'+ F.Name) then
          begin
            SearchFiles(SearchPath +'\'+ F.Name, FileName, List)
          end;    end;
        FindClose(F);
      end;
      Result := True;
    end;end.相关连接:http://expert.csdn.net/expert/topic/866/866683.xml
    Demo: SearchFiles('D:\', '*.*', Memo1.Lines);
      

  4.   

    unit FilesManage;interfaceuses SysUtils, FileCtrl, Classes;  function SearchFiles(const SearchPath: String; const FileName: String; List: TStrings): Boolean;implementationfunction IsDirectory(const Name: String): Boolean;
    begin
      Result := DirectoryExists(Name);
    end;function IsDot(const Name: String): Boolean;
    begin
      Result := (Name = '.')or(Name = '..');
    end;function SearchFiles(const SearchPath: String; const FileName: String; List: TStrings): Boolean;
    var
      Attr: Integer;
      F: TSearchRec;
    begin
      Attr := faAnyFile;
      if FindFirst(SearchPath + '\' + FileName, Attr, F) = 0 then
      begin
        if not IsDot(F.Name) then
          List.Add(SearchPath + '\' + F.Name);    while FindNext(F) = 0 do
        begin
          if not IsDot(F.Name) then
            List.Add(SearchPath + '\' + F.Name);
          if not IsDot(F.Name)and IsDirectory(SearchPath +'\'+ F.Name) then
          begin
            SearchFiles(SearchPath +'\'+ F.Name, FileName, List)
          end;    end;
        FindClose(F);
      end;
      Result := True;
    end;end.相关连接:http://expert.csdn.net/expert/topic/866/866683.xml
    Demo: SearchFiles('D:\', '*.*', Memo1.Lines);
      

  5.   

    以下刚整理了一下,完整代码unit1.dfm
    ---------------------------object Form1: TForm1
      Left = 214
      Top = 105
      Width = 696
      Height = 480
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      OnCreate = FormCreate
      PixelsPerInch = 96
      TextHeight = 13
      object cbDrives: TComboBox
        Left = 32
        Top = 16
        Width = 89
        Height = 22
        Style = csOwnerDrawFixed
        ItemHeight = 16
        TabOrder = 0
        OnClick = cbDrivesClick
      end
      object TreeView: TTreeView
        Left = 32
        Top = 64
        Width = 641
        Height = 337
        Indent = 19
        TabOrder = 1
        OnExpanding = TreeViewExpanding
      end
    end
    unit1.pas
    --------------------
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls, StdCtrls;type
      TForm1 = class(TForm)
        cbDrives: TComboBox;
        TreeView: TTreeView;
        procedure FormCreate(Sender: TObject);
        procedure TreeViewExpanding(Sender: TObject; Node: TTreeNode; var AllowExpansion: Boolean);    function BuildTree(APath: string; AItem: TTreeNode) : Boolean;
        procedure cbDrivesClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
      I: Integer;
      Drive: PChar;
    begin
        for I := 0 to 31 do
        begin
          if Boolean(GetLogicalDrives and (1 SHL I)) then
          begin
            Drive := PChar(CHR(65 + I) + ':\');
            cbDrives.Items.Add(Drive);
            if I = 2 then cbDrives.ItemIndex := cbDrives.Items.Count - 1;
          end;
      end;
    end;function TForm1.BuildTree(APath: string; AItem: TTreeNode) : Boolean;
    var
      SearchRec, Dummy: TSearchRec;
      Found: Integer;
      Node: TTreeNode;
    begin
      Found := FindFirst(APath + '*.*', faAnyFile, SearchRec);
      try
        while Found = 0 do
        begin
          if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
          begin
            if AItem <> nil then
              Node := TreeView.Items.AddChild(AItem, SearchRec.Name)
            else
              Node := TreeView.Items.Add(nil, SearchRec.Name);
            Node.StateIndex := 8;
            if SearchRec.Attr and faDirectory <> 0 then
            begin
              Node.HasChildren := FindFirst(APath + SearchRec.Name + '\*.*', faAnyFile, Dummy) = 0;
              Node.StateIndex := 6;
            end;
          end;
          Found := FindNext(SearchRec);
        end;
      finally
        FindClose(SearchRec);
      end;
      Result := True;
    end;procedure TForm1.TreeViewExpanding(Sender: TObject; Node: TTreeNode; var AllowExpansion: Boolean);  function GetNodeFullPath(Node: TTreeNode): String;
      begin
        Result := '';
        while Node <> nil do
        begin
          if Node.HasChildren then
            Result := '\' + Result;
          Result := Node.Text + Result;
          Node := Node.Parent;
        end;
        Result := cbDrives.Text + Result;
      end;begin
      if Node.HasChildren and (Node.Count = 0) then
        BuildTree(GetNodeFullPath(Node), Node);
    end;procedure TForm1.cbDrivesClick(Sender: TObject);
    begin
      BuildTree(cbDrives.Text, nil);
    end;end.要完整展开也行,将此TreeView如下操作即列出了整个硬盘目录
      TreeView.Items.BeginUpdate;
      TreeView.FullExpand;
      TreeView.Items.EndUpdate;稍加改进就可以把驱动器改为任一目录,这就不用我再多言了