Solved

What's the best way to know the size of a directory and a file?

Posted on 1998-09-04
6
161 Views
Last Modified: 2010-04-04
What's the best way to know the size of a directory and a file?
I want to know the size of a directory or the size of the files which are in this directory.
What's the best way to determine the size ?
By using :
Filesize
TFileStream...
0
Comment
Question by:nk51
  • 3
  • 2
6 Comments
 

Author Comment

by:nk51
ID: 1338623
I'm using Delphi 3.0
0
 
LVL 2

Expert Comment

by:freter
ID: 1338624
You'll need to use the FindFirst / FindNext function set to grab all the files in a dir. These two functions return a record which contains the file size information (and many other vital information).

rgds, Freter
0
 
LVL 5

Accepted Solution

by:
inter earned 50 total points
ID: 1338625
Hi,
You can do it with the following recursive procedure -I have written the code-

function SizeOfDir(Path : String):integer;
var
  SearchRec : TSearchRec;
  res       : integer;
begin
  Result := 0;
  res := FindFirst(Path+'\*.*', faAnyFile, SearchRec);
  while res = 0 do
  begin
      if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
      begin
        res := FindNext(SearchRec);
        continue;
      end;
      if SearchRec.Attr and faDirectory > 0 then
         Inc(Result, SizeOfDir(Path+'\'+SearchRec.Name))
      else Inc(Result, SearchRec.Size);
      res := FindNext(SearchRec);
      Application.ProcessMessages;
  end;
end;

procedure TDemoForm.Button1Click(Sender: TObject);
begin
  DirSize := 0;
  Caption := IntToStr(SizeOfDir('c:\winnt')); //do not add \ to the end of dir name!!!
end;

Determining size of file is much easiser for example

function SizeOfFileX(S : string):integer;
var
  T : TFileStream;
begin
  Result := 0;
  T := TFileStream.Create(S, fmOpenRead);
  try
     Result := T.Size;
  finally
    T.Free;
  end;
end;

Regards, Igor
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:nk51
ID: 1338626
Thanks
0
 
LVL 5

Expert Comment

by:inter
ID: 1338627
Soryy,
ignore the DirSize := 0 line above
0
 

Author Comment

by:nk51
ID: 1338628
ok
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now