Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
454 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Calculate Size of a Directoy and Sub-DirectoriesFormat this article printer-friendly!Bookmark function is only available for registered users!
DirSize()
Product:
Delphi 5.x (or higher)
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
11/16/2004
Search Keys:
delphi delphi3000 article borland vcl code-snippet DIRECTORY SIZE FILES
Times Scored:
6
Visits:
3630
Uploader: Mike Heydon
Company: EOH
Reference: mheydon@pgbison.co.za
 
Question/Problem/Abstract:
This function returns the size in bytes of the total files in a given directory path. The function parameters are ...

ADirName : string
This is the name path to the desired directory. It can include or exclude the trailing backslash as it is resolved internally.

ARecurseDirs : boolean = true
This denotes whether to recurse Sub-Directories or not. By default Sub-Directories will recursively be processed.

Examples

    iSize := DirSize('c:\temp');   // Will recurse sub-dirs
    iSize := DirSize('c:\temp\',true)  // Same as above

    iSize := DirSize('\program files\',false);  // No sub-dir recurse

Answer:



// ===========================================
// Return Total size of files in directory
// in bytes
// ===========================================

function DirSize(const ADirName : string;
                 ARecurseDirs : boolean = true) : integer;
const FIND_OK = 0;
var iResult : integer;

  // ====================
  // Recursive Routine
  // ====================
  procedure _RecurseDir(const ADirName : string);
  var sDirName : string;
      rDirInfo : TSearchRec;
      iFindResult : integer;
  begin
    sDirName := IncludeTrailingPathDelimiter(ADirName);
    iFindResult := FindFirst(sDirName + '*.*',faAnyFile,rDirInfo);

    while iFindResult = FIND_OK do begin
      // Ignore . and .. directories
      if (rDirInfo.Name[1] <> '.') then begin
        if (rDirInfo.Attr and faDirectory = faDirectory) and
          ARecurseDirs then
            _RecurseDir(sDirName + rDirInfo.Name) // Keep Recursing
        else
          inc(iResult,rDirInfo.Size);             // Accumulate Sizes
      end;

      iFindResult := FindNext(rDirInfo);
      if iFindResult <> FIND_OK then FindClose(rDirInfo);
    end;
  end;

// DirSize Main
begin
  Screen.Cursor := crHourGlass;
  Application.ProcessMessages;
  iResult := 0;
  _RecurseDir(ADirName);
  Screen.Cursor := crDefault;

  Result := iResult;
end;






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
error message!
    Luis Ortega (Dec 21 2004 3:07PM)

[Error] Unit1.pas(35): Undeclared identifier: 'IncludeTrailingPathDelimiter'
Respond

RE: error message!
Mike Heydon (Jan 10 2005 12:32PM)

IncludeTrailingPathDelimiter is a Delphi 7 function. Use IncludeTrailingBackslash() for older versions

DELPHI DOC ..
IncludeTrailingBackslash ensures that a path name ends with a trailing path delimiter ('\" on Windows, '/' on Linux). This function is included for backward-compatibility only. New programs should use IncludeTrailingPathDelimiter.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. DSpirito
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)