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
RaymondAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.