• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Determine a folder has taken up how much space

I want to determine how much space a specified folder has taken up, just like when you right click on a folder of Windows Explorer and click Properties, you can see the space being taken up by that folder.

Note: This function should work on Win98 or above.

Thanks.
R
0
Raymond
Asked:
Raymond
  • 3
  • 3
1 Solution
 
SJohnsonCommented:
Hi raymng,

function GetFolderSpace(Root: String; Recurse: Boolean): Double;
var
  SR: TSearchRec;
   R: Integer;
begin
  Result := 0;
  Root := IncludeTrailingBackSlash(Root);
  R := FindFirst(Root + '*.*', faAnyFile, SR);
  while R = 0 do
    begin
      if ((SR.Attr and faDirectory) = faDirectory) and Recurse then
        begin
          if SR.Name[1] <> '.' then
            Result := Result + GetFolderSpace(Root + SR.Name, Recurse);
        end
      else
        Result := Result + SR.Size;
      R := FindNext(SR);
    end;
  FindClose(SR);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  C: Double;
begin
  C := GetFolderSpace('C:\temp', true);
  Caption := FloatTOStr(C);
end;

How does that look?

Cheers,

Stu
0
 
RaymondAuthor Commented:
Hello,

Does it work on Win98 ?

How about if I don't want to include subfolder ?

Thanks.

R
0
 
RaymondAuthor Commented:
I encountered another problem. As I want to know the size in MB, so I tired

C := C Div 1024;

But it doesn't allow Double type division...  any idea ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
geobulCommented:
Hi,

>Does it work on Win98 ?
Yes.

>How about if I don't want to include subfolder ?
 C := GetFolderSpace('C:\temp', FALSE);

>But it doesn't allow Double type division...  any idea ?
C := Int(C / 1024); // in KB
C := Int(C / 1024 / 1024); // in MB

Regards, Geo
0
 
SJohnsonCommented:
>Does it work on Win98 ?
Yes.  Just test it.  It works on all 32 bit variation of windows.

>How about if I don't want to include subfolder ?
Sorry, I thought that was obvious.  Like Geo said, just set the second parameter to false (recursive = false).  It will then just process the passed folder.

DIV will only work with Integer types.  Otherwise you have to use '/'.

try:

var
  C: Double;
  S: String;
begin
  C := GetFolderSpace('C:\temp', true);
  S := format('%8.2f Mb', [C / 1024 / 1024]);
  Caption := S;
end;

Cheers,

Stu
0
 
RaymondAuthor Commented:
Thanks all here.

R
0
 
SJohnsonCommented:
Thanks for that, raymng.

Stu
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now