Solved

# Determine a folder has taken up how much space

Posted on 2004-11-07
199 Views
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
Question by:raymng

LVL 1

Accepted Solution

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
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

Author Comment

Hello,

Does it work on Win98 ?

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

Thanks.

R
0

Author Comment

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

LVL 17

Expert Comment

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

LVL 1

Expert Comment

>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

Author Comment

Thanks all here.

R
0

LVL 1

Expert Comment

Thanks for that, raymng.

Stu
0

## Featured Post

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!