Click here to Skip to main content
12,830,669 members (43,129 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 31 Jan 2014

A Directory Size Utility

, 31 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A little command line utility that will tell you the total size of all files in a directory


This is a little command line utility that will tell you the total size of all files in a directory. It traverses all sub-directories recursively and adds the size of all files - including hidden and system files - in them. Any inaccessible files and directories are ignored.

You can do the same thing with a Windows PowerShell command or with a for loop in a batch script, but this method seemed easier and more customizable to me.


This is written using Visual Studio 2013 Express, on .NET Framework 4. Earlier versions of the framework did not support a feature that I will mention later.

Getting the files

We use the DirectoryInfo class to recursively enumerate files in sub-directories.

DirectoryInfo di = new DirectoryInfo(directory);
IEnumerable<FileInfo> files = di.EnumerateFiles("*", SearchOption.AllDirectories);

Note that SearchOption.AllDirectories requests files in all subdirectories recursively. Once you do this, it is a simple matter of traversing the list using its enumerator.

UInt64 size = 0;
IEnumerator<FileInfo> e = files.GetEnumerator();
while (e.MoveNext)
    size += (UInt64)e.Current;

The Catch

The code above is file if all files are accessible. That is, if the current user has read access rights to all files in the sub-directories. However, if not, e.MoveNext throws an UnauthorizedAccesssException. To get past this problem, we surround e.MoveNext with a try/catch block and ignore any UnauthorizedAccessExceptions.

The modified code is given below.

while (true)
       if (!e.MoveNext())
    catch (UnauthorizedAccessException)
        continue; // ignore exception   
    FileInfo fi = e.Current;
    size += (UInt64)fi.Length;

Older .NET versions

Previous versions of DirectoryInfo do not have an EnuerateFiles method, instead it has GetFiles which returns an array of FileInfo. Unlike EnuerateFiles, this immediately throws a security exception if any sub-directory or file is inaccessible, which means we'd have to manually enumerate each file in the current directory, then recursively get sub-directories and files in them. I haven't tried this yet, maybe I'll get around to it some time in a future post.


I haven't tested this much, it'll probably crash under circumstances I didn't foresee. Please leave a comment in that case. Alternative methods are also welcome.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Japan Japan
No Biography provided

You may also be interested in...


Comments and Discussions

SuggestionIgnoring UnauthorizedAccessException Pin
dandy7231-Jan-14 8:01
memberdandy7231-Jan-14 8:01 
GeneralRe: Ignoring UnauthorizedAccessException Pin
Indivara31-Jan-14 13:51
subeditorIndivara31-Jan-14 13:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 31 Jan 2014
Article Copyright 2014 by Indivara
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid