Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i get the size of a folder 3

Status
Not open for further replies.

MirceaVleju

Programmer
Oct 10, 2003
32
0
0
RO
Is there a function that gives the total size of a folder?
 
I couldn't find anything on getting the size of the folder, but you can get the size of a file. Perhaps you could loop through all the files in the folder and add them up?



Leslie
 
Leslie's approach is correct: Here's some sample code.


var
f : TSearchRec;
x : Integer; // Hold total size of folder
begin
x := 0;
if FindFirst('*.*', faAnyFile-faDirectory, f) = 0 then
repeat
inc(x, f.Size);
until FindNext(f) <> 0;
FindClose(f);
end;


Note that this will only find files in the current directory. Post back if you need help modifying this to search through subdirectories as well.
 
To get subfolders as well, use a recursive function:

function GetTreeSize(const ASTR_RootDir:String):Integer;
var
LREC_SearchRec : TSearchRec;
LINT_Erc : Integer;
LINT_TreeSize: Integer;
begin
LINT_TreeSize := 0;
If (ASTR_RootDir = '') then
Exit;

ChDir(ASTR_RootDir);

FindFirst('*.*', faAnyFile, LREC_SearchRec);
LINT_Erc := 0;
while LINT_Erc = 0 do begin
if ((LREC_SearchRec.Name <> '.' ) and (LREC_SearchRec.Name <> '..')) then
begin
if (LREC_SearchRec.Attr and faDirectory>0) then
LINT_TreeSize := LINT_TreeSize + GetTreeSize(ASTR_RootDir + LREC_SearchRec.Name + '\') //Recursive call
else
LINT_TreeSize := LINT_TreeSize + LREC_SearchRec.Size; //Found a file. Get the size.
end;

LINT_Erc := FindNext(LREC_SearchRec);
end;
FindClose(LREC_SearchRec);
if Length(ASTR_RootDir) > 3 then
ChDir('..');
result := LINT_TreeSize;
end;


Brian
&quot;There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group.&quot; - tag line I stole
 
Wow you guys are good! I could come up with some theory, but.....Brian, did you have that code from an earlier project or did you come up with on the fly? I can't wait til I can code like that.

Have a star on me!

leslie
 
That is from a project we have, and it was altered from code that deleted all the files in the directory/subdirectory that we got off the internet.

Brian
&quot;There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group.&quot; - tag line I stole
 
There is a little bit of baggage from the previous incarnation of bbegley's function. For example, there is no need to call ChDir.

The following code does the job:
Code:
function GetTreeSize ( path: string ): integer;
var
 tsr: TSearchRec;
begin
 result := 0;
 path := IncludeTrailingBackSlash ( path );
 if FindFirst ( path + '*', faAnyFile, tsr ) = 0 then begin
  repeat
   if ( tsr.attr and faDirectory ) > 0 then begin
    if ( tsr.name <> '.' ) and ( tsr.name <> '..' ) then
     inc ( result, GetTreeSize ( path + tsr.name ) );
   end
   else
    inc ( result, tsr.size );
  until FindNext ( tsr ) <> 0;
  FindClose ( tsr );
 end;
end;

Andrew
 
That's nice. I have never seen repeat..until used before. For some reason, I've never seen inc() or IncludeTrailingBackslash() either. We have a family of directory functions (delete/copy/find/etc...) that will be much cleaner as a result of your post. Star for you.



Brian
&quot;There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group.&quot; - tag line I stole
 
Look i already know about the recursive function beacuse i had tryed it before i asked this quetion.The problem is that it takes a long time to skan the entire folder and subfolder (if the folder is large) and if i want just the size i have to wait.Now in windows Xp when you click on a folder it showes the size and the files in that folder.And it does that after just a second?
Is there an Api function that gets the size of the folder?
 
Well in your first post all you asked was how to do it, please don't come back and be rude to those of us who took time out to respond to your question.

Maybe if your original question was:

&quot;I have tried using this function but it takes a really long time, is there any other way to achieve this&quot;.

You may have gotten alternatives to this method. But you got exactly what you asked for.
 
Look Lespaul. I'm soory that my second message ofended you.I did=t mean to be rude!
All i asked for is a function that gives the size of the folder.In my second message i pointed out the fact that i knew of this solution and that i wanted to know is there a quicker way!OK!
 
I'm pretty sure you're mistaken 04121986. The first time I try to get the size of my Program Files folder for example (in Windows XP), it counts through all the folders. Closing the dialog box and retrying will return the total instantly, because XP (and all other versions of Windows) use disk caching. Technically - the recursive function above will also take advantage of disk caching, and return the same result quicker the second time around.

Network drives are different however, as disk caching cannot be used on them locally, and they will generally always need to be searched directly again every time.

In terms of efficiency, there's no other way to get the size of the folder. No API could ever get the folder size any more efficiently unless it used a quicker routine to get the size of an individual file. The total size of each folder is not stored anywhere on the disk.

I hope that helps you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top