使用Delphi获取一个文件夹所占空间大小

使用下面的函数,它使用递归算法查找所有属性的文件(包括隐藏、系统、存档等)在该文件夹及子文件夹。
仅仅使用一个文件夹路径作为参数。

Try the following function (it looks at hidden, system, archive, and normal files; it uses a recursive algorithm to look in all sub-directories also; just supply a starting directory as a parameter.Copy From:http://www.qfly.cn
[separater]

[quote]
var
   DirBytes : integer;
function FolderSize(Dir:string):integer;
var
   SearchRec : TSearchRec;
   Separator : string;
begin
   Result:=0;
   if Copy(Dir,Length(Dir),1)='/' then
     Separator := ''
   else
     Separator := '/';
   if FindFirst(Dir+Separator+'*.*',faAnyFile,SearchRec) = 0 then begin
     if FileExists(Dir+Separator+SearchRec.Name) then begin
       DirBytes := DirBytes + SearchRec.Size;
     end else if DirectoryExists(Dir+Separator+SearchRec.Name) then begin
       if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then begin
         FolderSize(Dir+Separator+SearchRec.Name) ;
       end;
     end;
     while FindNext(SearchRec) = 0 do begin
       if FileExists(Dir+Separator+SearchRec.Name) then begin
         DirBytes := DirBytes + SearchRec.Size;
       end else if DirectoryExists(Dir+Separator+SearchRec.Name) then
       begin
         if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then begin
           FolderSize(Dir+Separator+SearchRec.Name) ;
         end;
       end;
     end;
   end;
   FindClose(SearchRec) ;
   Result:=DirBytes;
end;
[/quote]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi中,要实现拖拽文件夹获取其路径,可以按照以下步骤进行操作: 1. 添加控件:在窗体上添加一个TListBox控件用于显示拖拽的文件夹路径。 2. 设置TListBox为可接受拖拽文件:在TForm的OnCreate事件中添加以下代码,将TListBox的DragMode属性设置为dmAutomatic,使其可以接受文件拖拽操作。 ``` procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.DragMode := dmAutomatic; end; ``` 3. 处理拖拽事件:在TListBox的OnDragDrop事件中添加以下代码,用于处理拖拽文件夹的操作。 ``` procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer); var i: Integer; DropFiles: TStrings; begin DropFiles := TStringList.Create; try if Source is TListBox then begin DropFiles.AddStrings(TListBox(Source).Items); for i := 0 to DropFiles.Count - 1 do ListBox1.Items.Add(DropFiles[i]); end; finally DropFiles.Free; end; end; ``` 4. 显示文件夹路径:在TListBox的OnDragOver事件中添加以下代码,用于在拖拽过程中显示文件夹路径。 ``` procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); begin Accept := Source is TListBox; if Accept then begin if State = dsDragEnter then ListBox1.Items.Clear; ListBox1.Items.Add(GetDroppedFileName); end; end; ``` 5. 获取文件夹路径:最后,我们还需要添加一个函数GetDroppedFileName,用于获取文件夹路径。 ``` function TForm1.GetDroppedFileName: string; var DropHandle: THandle; FileCount, i: Integer; FileName: array[0..MAX_PATH] of Char; begin Result := ''; DropHandle := DragQueryFile(DragQueryFile(DragQueryFile(DragQueryFile(Handle, Cardinal(-1), nil, 0), 0, nil, 0), 0, @FileName[0], Length(FileName)), 0, nil, 0); if DropHandle <> Cardinal(-1) then begin FileCount := DragQueryFile(Handle, DragHandle, nil, 0); if FileCount > 0 then begin DragQueryFile(DragHandle, 0, @FileName[0], Length(FileName)); Result := FileName; end; end; DragFinish(DragHandle); end; ``` 以上就是在Delphi使用拖拽操作获取文件夹路径的步骤和代码。希望能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值