I saw an interesting question on Experts Exchange last week. The guy wanted to loop through all the folders (including subfolders) to discover how many files are stored in them and how big are the files.

It is not a complex one but it requires a bit of recursion to loop through the folders, a bit of error handling because there are folders that might be protected.

So here comes the first article of January!

Downloadable code

This month solution contains both VB and C# projects. The solution was created using Visual Studio 2017 RC but the code should work starting with the .Net Framework 4.0 (Visual Studio 2010).

Figure 1: The demo application in action

Very simple interface

Only 3 controls are required to build this demo application. A button to launch the process. A label to show some progression. A datagrid to show the result.

Storing the results in memory

Before showing the results in the datagrid, we need a way to store the values. I wanted something simple. What is easiest than a structure? And to hold the full list of, a simple generic List of that structure and we will be in business.

So this LoadFolderSize method will be called recursively to loop through all the folders and subfolders of a starting point (c:\ in my case).

Because the process can be quite long, the first line of this method is to show the folder being processed in the label. This method will be called in a tight loop. If we want to see the label being refreshed, we need to call the DoEvents method.

The next block of lines creates a DirectoryInfo instance with the current folder. The path, the number of files, and the total size (in bytes) is added to the list of folder.

Once we have the data for the current folder, we can loop through the list of directories and for each one of them, recall the very same method we are in passing in arguments the path of this sub-directory. If we cannot get access, an exception is raised and an entry with -1 in the NumberOfFiles and FileSize is added to the list.

With this little example, you have the base of an application you can get a lot further. One example would be to store the results in a file or database to be able to compare against another execution (say you run it on a weekly basis). You would then be able to easily detect which folder got the biggest increase (or decrease) during that period.

I will surely expand this sample further to monitor network folders if I get a couple of free hours (that will surely not happen in the next few months!).

Conclusion

Sometime, a sample question on forums requires a bit of thinking and simple code to put in place.