Snowy
Snowy

Reputation: 6122

Find all Folders with Certain Size Parameters?

I am looking for a good way to find all folders that have contents that are say 500k or less. I think this involves recursively going to the lowest folder in a hierarchy and then "reading up" to get a size total. All ideas appreciated (something int he .NET framework or PowerShell if I really have to), thanks.

Upvotes: 1

Views: 230

Answers (1)

Michael D. Irizarry
Michael D. Irizarry

Reputation: 6302

This could help you out.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GetDirectorySize
{
    class Program
    {
        static void Main(string[] args)
        {
            long maxFolderSizeInBytes = 20000000;

            foreach (var directory in Directory.GetDirectories(@"C:\Projects\Visual Studio 2010\"))
            {
                string[] a = Directory.GetFiles(directory, "*.*");
                long i = 0;
                foreach (string name in a)
                {
                    FileInfo info = new FileInfo(name);
                    i += info.Length;
                }
                if (i <= maxFolderSizeInBytes)
                { 
                    Console.WriteLine(directory);
                }
            }
            Console.ReadLine();
        }
    }
}

Upvotes: 1

Related Questions