Free your disk: cleaning up the bin/debug folder.

An empty LightSwitch project without any tables or screens, takes up about 195 Mb.  Like most Visual Studio projects, a lot of this space is taken up by the bin/debug folder, containing mostly compiled binaries that are only useful when you’re actually working on the project.

No biggie normally, since even cheap hard disks have hundreds of Gb of room nowadays.  However, much to my own surprise, I ran into out of available hard disk space today while installing the JAVA SDK.

Yes, I was completely surprised about the fact that I’m being forced to help out on some of the JAVA projects for a couple of days, but hey nothing I can do about that…  About the disk space however…

There’s an awesome tool called CleanProject, which you should really get if you ever share sources (keep those samples coming on MSDN!), but it fails to scan an entire disk… After searching online for at least 7 seconds, I decided to create my own: “LightSwitch and other project’s bing/debug folder sweeper” …

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

    class Program
    {
        static readonly DirectoryInfo[] empty = new DirectoryInfo[] { };

        class DriveComparer : IEqualityComparer<System.IO.DriveInfo>
        {

            public bool Equals(System.IO.DriveInfo x, System.IO.DriveInfo y)
            {
                return x.DriveType.Equals(y.DriveType) && x.VolumeLabel.Equals(y.VolumeLabel);
            }

            public int GetHashCode(System.IO.DriveInfo obj)
            {
                return obj.VolumeLabel.GetHashCode();
            }
        }

        static void Main(string[] args)
        {
            foreach (var drive in DriveInfo.GetDrives()
                .Where(d => d.DriveType == DriveType.Fixed && d.IsReady)
                .Distinct(new DriveComparer())
                .Select(d => d.RootDirectory)
                )
            {
                Console.WriteLine("Scanning for bin/debug folders on drive " + drive + ". (This might take a while)");

                WalkDirectoryTree(drive);
            }
            Console.WriteLine("All done, thanks for watching! - Press any key to exit.");
            Console.ReadKey(true);
        }

        static void WalkDirectoryTree(System.IO.DirectoryInfo root)
        {
            System.IO.DirectoryInfo[] subDirs = null;
            try
            {
                subDirs = root.GetDirectories();
            }
            catch (UnauthorizedAccessException)
            {
                //...
                subDirs = empty;
            }
            foreach (System.IO.DirectoryInfo dirInfo in subDirs.Where(s => !s.Name.ToLower().Contains("recycle.bin")))
            {
                if (root.Name.ToLower().Equals("bin")
                    && dirInfo.Name.ToLower().Equals("debug"))
                {
                    Console.WriteLine("Cleaning up : " + dirInfo.FullName);
                    if (!dirInfo.Empty()) {
                        Console.WriteLine("WARNING! Partially or completely failed the clean up.");
                    }
                }
                else
                {
                    WalkDirectoryTree(dirInfo);
                }
            }

        }
    }

    static class Ext {
        public static bool Empty(this System.IO.DirectoryInfo directory)
        {
            bool succes = true;
            foreach (System.IO.FileInfo file in directory.GetFiles())
                try { file.Delete(); }
                catch (UnauthorizedAccessException) { succes = false; }
            foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories())
                try { succes = succes && subDirectory.Empty(); }
                catch (UnauthorizedAccessException) { succes = false; }

            try{ directory.Delete(true); }
            catch (UnauthorizedAccessException) { succes = false; }

            return succes;
        }
    }

Your typical “get all local hard disks, loop recursively over subfolders, emtpy the bin/debug folders completely”.  After running for about 25 minutes (from Visual Studio with breakpoints enables), two hard disks were completely scanned & I now own an extra 13 Gb of free disk space!  More than enough to fit that JAVA SDK… Darn…

If you would clean your disks, let me know how many bits you freed!

Use at own risk. *Code posted for my own personal use.  I’d keep the sample somewhere on my local disk instead of sharing, but no bit on my local disk seems to be safe from accidental deletion…* 

About these ads

6 thoughts on “Free your disk: cleaning up the bin/debug folder.

  1. Pingback: Beth Massi - Sharing the goodness

  2. Pingback: LightSwitch Community & Content Rollup–February 2013 | MSDN Blogs

  3. Thanks for your personal marvelous posting! I actually enjoyed reading it, you will be a great author.I will
    be sure to bookmark your blog and definitely will come back later in life.
    I want to encourage you to ultimately continue your great
    work, have a nice afternoon!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s