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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining the size of a directory ?

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
Is there a simple way of determining the size of an indicated directory ?
That is, how would I set up a function (or does one exist that I'm overlooking) to pass a path for a directory (as a string) and obtain the size (in KB or MB) of said directory ?
I guess it can be done but I can't see it for looking.
Thanks in advance
Steve
 
on a Windows NT/2000 system with NTFS :

var h:thandle;

h := createfile('c:\winnt',GENERIC_READ,FILE_SHARE_READ or FILE_SHARE_WRITE,NIL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0);

label1.caption := inttostr(getfilesize(h,nil));

closehandle(h);
 
hi,

try this code.

{
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.
}

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
DirSize(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
DirSize(Dir+Separator+SearchRec.Name);
end;
end;
end;
end;
FindClose(SearchRec);
Result:=DirBytes;
end;


Steph [Bigglasses]
 
Here's my function :)
Code:
function GetDirectorySize(const DirName : String): Cardinal;
var
  BaseDirName    : String;
  ASearchRec     : TSearchRec;
begin
  BaseDirName := ExcludeTrailingBackslash(DirName);
  Result := 0;
  FindFirst(Format('%s%s', [BaseDirName, '\*.*']), faAnyFile, ASearchRec);
  try
    while (ASearchRec.Name = '.')or(ASearchRec.Name = '..') do
     if (FindNext(ASearchRec) <> 0) then Exit;
    repeat
      if ((ASearchRec.Attr and faDirectory) > 0) then
       Result := Result + GetDirectorySize(Format('%s%s%s', [BaseDirName, '\', ASearchRec.Name]), Result)
      else
       Inc(Result, ASearchRec.Size);
    until (FindNext(ASearchRec) <> 0);
  finally
    FindClose(ASearchrec);
  end;
end;
Cheers.

--- markus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top