[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 1998-09-04
6
Medium Priority
?
170 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 150 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

872 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