Click here to Skip to main content
6,634,665 members and growing! (15,668 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » File System     Intermediate

Scan the Directory / Files (Calculating the size)

By Srinivas Varukala

Scans through the directory recursively and calculates the total size.
C++, C#.NET 1.0, Win2K, WinXP, Win2003, MFC, Dev
Posted:17 Apr 2004
Views:74,800
Bookmarked:27 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 3.34 Rating: 2.45 out of 5
6 votes, 26.1%
1
3 votes, 13.0%
2
1 vote, 4.3%
3
7 votes, 30.4%
4
6 votes, 26.1%
5

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:

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:

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:

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:

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

About the Author

Srinivas Varukala


Member
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
Occupation: Web Developer
Location: United States United States

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
GeneralGreat app PinmemberMember 371701123:10 16 Mar '08  
Generala typo PinmemberArfeeng12:59 13 Jan '06  
GeneralRe: a typo PinmemberSrinivas Varukala13:43 13 Jan '06  
GeneralRe: a typo Pinmemberryelpango17:04 11 Aug '09  
Generalgood PinsussAnonymous18:27 18 Apr '04  
GeneralYou can improve. PinmemberMr.Prakash16:11 18 Apr '04  
GeneralRe: You can improve. PinmemberSrinivas Varukala16:53 18 Apr '04  
GeneralRe: You can improve. PinsitebuilderPaul Watson5:48 12 May '04  
GeneralRe: You can improve. PinmemberMr.Prakash7:57 12 May '04  
GeneralRe: You can improve. PinsussAnonymous8:45 12 May '04  
GeneralRe: You can improve. PinmemberMr.Prakash7:20 13 May '04  
GeneralTry using delegates PinmemberMark Focas15:31 18 Apr '04  
GeneralRe: Try using delegates PinmemberSrinivas Varukala16:54 18 Apr '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Apr 2004
Editor: Smitha Vijayan
Copyright 2004 by Srinivas Varukala
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project