Solved

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

Posted on 1998-09-04
6
160 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
Comment Utility
I'm using Delphi 3.0
0
 
LVL 2

Expert Comment

by:freter
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:nk51
Comment Utility
Thanks
0
 
LVL 5

Expert Comment

by:inter
Comment Utility
Soryy,
ignore the DirSize := 0 line above
0
 

Author Comment

by:nk51
Comment Utility
ok
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

10 Experts available now in Live!

Get 1:1 Help Now