Click here to Skip to main content
15,113,970 members
Articles / Desktop Programming / MFC
Article
Posted 17 Apr 2004

Stats

118.7K views
1.2K downloads
38 bookmarked

Scan the Directory / Files (Calculating the size)

Rate me:
Please Sign up or sign in to vote.
2.56/5 (26 votes)
17 Apr 2004
Scans through the directory recursively and calculates the total size.

Screenshot

Introduction

It's a simple program which scans a directory recursively through the subfolders and sum up the size of the files. It also displays the size of the individual files, and eventually the total size of the directory.

Explanation

Observe the follows:

C#
using System;
using System.IO;

We need to include System.IO, as we are working on Files and/or Directories.

The program starts as follows:

C#
static void Main(string[] args) 
{
  //Creates an object of the class getFilesInfo 
  //and calls its function displayFileInfo()
  //The argument is passed to that function 
  //which is the path to a directory or a file.
  getFilesInfo obj = new getFilesInfo();
  obj.displayFileInfo(args[0]);

  // displayFileInfo() is the function which does everything and 
  // sets the static variable totsize to the total size of the dir/file
  //you passed as the argument.
  Console.WriteLine("Total Size = {0}", totsize);
}

Now, let's look at the crux of the program:

C#
protected void displayFileInfo(String path)
{
  try
  {
    //Checks if the path is valid or not
    if(!Directory.Exists(path))
    {
      Console.WriteLine("invalid path");
    }
    else
    {
      try
      {
        //Directory.GetFiles() returns an array 
        //of strings which are not just
        //the file/directory name but the whole path to that
        //file folder.
        string[] fileList = Directory.GetFiles(path);
        for(int i=0; i {
          if(File.Exists(fileList[i]))
          {
            //File Info is a Class which extend FileSystemInfo class. 
            FileInfo finfo = new FileInfo(fileList[i]);
            //finfo.Length returns the File size. 
            totsize += finfo.Length;
            Console.WriteLine("FILE: "+fileList[i]+" :Size>"+ finfo.Length); 
          }
        }
      }
      catch( System.NotSupportedException e1)
      {
        Console.WriteLine("Error1"+e1.Message);
      }
      try
      {
        string[] dirList = Directory.GetDirectories(path);
        for(int i=0; i {
          Console.WriteLine("DIRECTORY CHANGED:" + dirList[i]); 
          //Call the function recursively to get 
          //the file sizes in the subfolder. 
          displayFileInfo(dirList[i]);
        }
      }
      catch( System.NotSupportedException e2)
      {
        Console.WriteLine("Error2:"+ e2.Message);
      }
    }
  }
  catch(System.UnauthorizedAccessException e)
  {
    Console.WriteLine("Error:"+ e.Message);
  }
}

You should have observed that we can't find the size of a folder directly. We should sum up the size of the files it constitutes. I think it is so because a Folder is just a pointer which points to the files and other directories. It has no size by itself. And one more thing is that the methods:

C#
Directory.GetFiles(path);
Directory.GetDirectories(path);

return not just the file/directory name but the whole path to that file/folder.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Srinivas Varukala
Web Developer
United States United States
My name is Srinivas Varukala, a citizen of India.
I m pursuing my masters in CS in the US of A.
My interests r in:
C
C++
C#
Java n its variants.
My webspace: www.cs.odu.edu/~svarukal

Comments and Discussions

 
QuestionOther ads free tools Pin
devvvy13-Feb-15 15:27
Memberdevvvy13-Feb-15 15:27 
There are so many shareware with annoying ads.

Here's two I found that are free + ads free:
a. Name of the tool is "Folder Size", has a GUI (let you drill down recursively) http://foldersize.sourceforge.net/

b. Command line tool appliedalgo.com "PlatformAuditProbe" (Forensic tool kit actaully), among other things it can log directories sizes:
PlatformAuditProbe AuditMode=LocalFolders Delimitor=; ResultFileName=CDrive.csv ConnectJobControlller=false


Specify folders you want monitor in "MonitoredLocalFolders.txt"
dev

GeneralMy vote of 1 Pin
David Crow14-Jan-10 10:42
MemberDavid Crow14-Jan-10 10:42 
GeneralGreat app Pin
sawo16-Mar-08 23:10
Membersawo16-Mar-08 23:10 
Generala typo Pin
Arfeeng13-Jan-06 12:59
MemberArfeeng13-Jan-06 12:59 
GeneralRe: a typo Pin
Srinivas Varukala13-Jan-06 13:43
MemberSrinivas Varukala13-Jan-06 13:43 
GeneralRe: a typo Pin
ryelpango11-Aug-09 17:04
Memberryelpango11-Aug-09 17:04 
Generalgood Pin
Anonymous18-Apr-04 18:27
MemberAnonymous18-Apr-04 18:27 
GeneralYou can improve. Pin
Prakash Nadar18-Apr-04 16:11
MemberPrakash Nadar18-Apr-04 16:11 
GeneralRe: You can improve. Pin
Srinivas Varukala18-Apr-04 16:53
MemberSrinivas Varukala18-Apr-04 16:53 
GeneralRe: You can improve. Pin
Paul Watson12-May-04 5:48
sitebuilderPaul Watson12-May-04 5:48 
GeneralRe: You can improve. Pin
Prakash Nadar12-May-04 7:57
MemberPrakash Nadar12-May-04 7:57 
GeneralRe: You can improve. Pin
Anonymous12-May-04 8:45
MemberAnonymous12-May-04 8:45 
GeneralRe: You can improve. Pin
Prakash Nadar13-May-04 7:20
MemberPrakash Nadar13-May-04 7:20 
GeneralTry using delegates Pin
Mark Focas18-Apr-04 15:31
MemberMark Focas18-Apr-04 15:31 
GeneralRe: Try using delegates Pin
Srinivas Varukala18-Apr-04 16:54
MemberSrinivas Varukala18-Apr-04 16:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.