how to get total bytes of all files from directory

Discussion in 'Software' started by AlbertM., Jul 29, 2008.

  1. AlbertM.

    AlbertM.

    Joined:
    Apr 2, 2008
    Messages:
    2
    Hi there,
    I am trying to get a total count of all my files in bytes. I retreived all the files with .Length but I don't know how to get total count. Any help?...

    Here is my working code:

    void Page_Load()
    {
    string folder = "C:\\Inetpub\\wwwroot\\mySite";
    Recurse(folder);
    }

    void Recurse(string folder)
    {
    System.IO.FileInfo fi = null;
    long size = 0;

    foreach (string f in Directory.GetFiles(folder))
    {
    string filename = Path.Combine(folder, f);

    fi = new FileInfo(f);
    size += fi.Length;
    Response.Write(filename + " - " + size / 1024 + " KB<br/>");
    }

    foreach (string d in Directory.GetDirectories(folder))
    {
    string path = Path.Combine(folder, d);
    Recurse(path);
    Response.Write(path + "<br/><br/>");
    }

    }
    AlbertM., Jul 29, 2008
    #1
    1. Advertising

  2. AlbertM.

    AlbertM.

    Joined:
    Apr 2, 2008
    Messages:
    2
    I found a solution. You see, my server provider contains various sites for various clients and what I wanted to do is get the size of "my server account" space. So I just had to get the Physical Path to my site which is "D:\\webs\\band46" (band46 is the root folder which they created for my account) and created the following:


    void Page_Load()
    {
    string physicalPath = "D:\\webs\\band46";
    GetFolderSize(physicalPath);
    }


    private double GetFolderSize(string physicalPath)
    {
    double dblDirSize = 0;
    DirectoryInfo objDirInfo = new DirectoryInfo(physicalPath);
    Array arrChildFiles = objDirInfo.GetFiles();
    Array arrSubFolders = objDirInfo.GetDirectories();

    //first loop through the files and add the size of each file
    foreach (FileInfo objChildFile in arrChildFiles) {
    dblDirSize += objChildFile.Length;
    }

    //then for each subfolder found call this function again
    foreach (DirectoryInfo objChildFolder in arrSubFolders) {
    dblDirSize += GetFolderSize(objChildFolder.FullName);

    //Used Space
    lblUsedSpace.Text = (Math.Round(dblDirSize/1024/1024)).ToString() + " MB";

    //Available Space
    int i = 750;
    string AvSpace = (i - Math.Round(dblDirSize/1024/1024)).ToString();
    lblAvailable.Text = AvSpace + " MB";

    if(Math.Round(dblDirSize/1024/1024) > 740)
    {
    lblLimitReached.Text = "Attention! You have reached your limit! ";
    }
    }

    return dblDirSize;
    }



    And that worked great for me!

    As you can also see...(as an extra, which is optional) I physically put 750MB as my account limit and if my Used Space (lblUsedSpace.Text) has reached above 740MB... I will be notified using the Label: lblLimitReached.Text

    If anyone has any input on how to clean up or make better code... please be free to reply.

    Thanks,
    Albert
    Last edited: Aug 6, 2008
    AlbertM., Aug 6, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. netproj
    Replies:
    0
    Views:
    448
    netproj
    Dec 21, 2005
  2. The Old Sourdough

    Re: receiving Bytes and sending Bytes

    The Old Sourdough, Jul 23, 2003, in forum: Computer Support
    Replies:
    0
    Views:
    429
    The Old Sourdough
    Jul 23, 2003
  3. Ieuan Adams

    Re: receiving Bytes and sending Bytes

    Ieuan Adams, Jul 24, 2003, in forum: Computer Support
    Replies:
    0
    Views:
    491
    Ieuan Adams
    Jul 24, 2003
  4. Ray Donzdorf

    Newsgroup Quotas - What to do to get more bytes?

    Ray Donzdorf, Jan 30, 2004, in forum: Computer Support
    Replies:
    6
    Views:
    584
  5. John Y
    Replies:
    2
    Views:
    533
    Pete @ www.GymRatZ.co.uk
    Feb 2, 2007
Loading...

Share This Page