Click here to Skip to main content
12,757,204 members (40,140 online)
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 26 Sep 2006

Disk Usage Viewer

, 3 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
A disk usage viewer with tree-view output.

DiskUse example screen image


This is a disk usage viewer, a tool that helps answer the question, "What is taking up all my disk space?" It displays a tree of folders (directories) starting from the specified folder, listing with each folder the size of all that it contains. Folders are listed in the order of decreasing size. The tree may be expanded to show layers below the topmost.


The inspiration for this tool is the Unix disk usage tool du. The lack of a satisfying, equivalent, commonly-available tool for Windows prompted me to write this one.

Using the code

Using the tool is straightforward:

  1. Click Select, and choose the drive or folder to examine.
  2. Expand any folder in the displayed list to see the details of its contents.
  3. Use the Copy menu to save a text representation of the displayed tree, or to copy the path to the selected folder.

The generated text representation of the tree shown is:

11.9 G  C:
   + 4.16 G  Program Files
   + 2.65 G  Documents and Settings
   + 2.51 G  WINNT
   + 1.08 G  Matt
   + 68.4 M  My Music
   + 28.3 M  dell
   + 19.8 M  Recycled
   + 18.4 M  My Downloads
   + 10.3 M  VXIPNP
   + 1.12 M  samples
   |  + 1.12 M  VC98
   |     + 1.12 M  mfc
   |        + 846 K  ole
   |        |  + 846 K  wordpad
   |        |     + 27 K  res
   |        |     + 0 K  UniDebug
   |        + 275 K  general
   |           + 275 K  LISTHDR
   |              + 33 K  res
   |              + 0 K  Release
   + 1 K  cygwin
   + 0 K  WUTemp

The code recursively explores the folder structure below the selected root folder, and populates a Windows tree control accordingly. The application is a single document (SDI) MFC app generated by AppWizard, with a CTreeView used for the view class.

Most of the functionality is implemented in the CDiskUseView class, which has close access to its underlying tree control. Event handlers for the menu operations appear in the CMainFrame class.

Points of interest

A tree, such as a drive's folder structure, is inherently a recursive structure, so working with a tree is a natural application for recursive programming techniques. The function ScanDirectory sums the sizes of the files in a directory, and calls itself recursively to find the sizes of directories it contains. The function DisplaySubtree writes a text representation of one folder in the tree, and calls itself recursively for the levels below. And the function ExpandTreeLevel calls itself recursively to adjust the tree control view so that one, two, or all levels are visible.

For a problem such as this one, using recursion realizes the desired functionality with a very small amount of code. The recursive approach may initially seem confounding and confusing, but it's not difficult. This small program can serve as an example of how it is done.


Thanks to S. Sokolenko, whose GetFolder wrapper for SHBrowseForFolder saved me the effort of learning the details of that operation.


  • Oct. 3, 2006: Update to handle directory names that include a dot.
  • Sept. 26, 2006: Created.


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


About the Author

Matt Fichtenbaum
Web Developer
United States United States
Electrical engineer turned software engineer, working in automated test equipment development. Enjoy partitioning hardware/software systems into logical, coherent structures. Incorrigible builder of tools.

You may also be interested in...


Comments and Discussions

GeneralMultithreaded Pin
cjbarth16-Mar-09 14:59
membercjbarth16-Mar-09 14:59 
GeneralRe: Multithreaded Pin
Matt Fichtenbaum16-Mar-09 15:56
memberMatt Fichtenbaum16-Mar-09 15:56 
It wouldn't be hard to make multithreaded. Move all the processing to another thread, and start that thread to do the scan. Appropriate GetSafeHwnd calls should be added to access the UI objects from the worker thread.

To perform multiple scans simultaneously, one could restructure the app as a multiple-document (MDI) app. But it's easier to run multiple copies of the program as it is. Smile | :)
General"rlivelyppk" missed the point Pin
jerryshirley12-Oct-06 20:55
memberjerryshirley12-Oct-06 20:55 
GeneralSupport for Commandline Arguments ... [modified] Pin
Martin081512-Oct-06 0:46
memberMartin081512-Oct-06 0:46 
GeneralNot all directories scanned Pin
~MyXa~10-Oct-06 2:02
member~MyXa~10-Oct-06 2:02 
GeneralRe: Not all directories scanned Pin
Matt Fichtenbaum11-Oct-06 3:02
memberMatt Fichtenbaum11-Oct-06 3:02 
GeneralSequoiaView Pin
~MyXa~10-Oct-06 1:45
member~MyXa~10-Oct-06 1:45 
GeneralRe: SequoiaView Pin
jauming25-Apr-09 5:46
memberjauming25-Apr-09 5:46 
Generali.Disk performs this function Pin
rlivelyppk4-Oct-06 10:05
memberrlivelyppk4-Oct-06 10:05 
GeneralRe: i.Disk performs this function Pin
Anonymuos4-Oct-06 10:43
memberAnonymuos4-Oct-06 10:43 
GeneralRe: i.Disk performs this function [modified] Pin
rlivelyppk4-Oct-06 10:52
memberrlivelyppk4-Oct-06 10:52 
GeneralA few suggestions Pin
Phil Atkin2-Oct-06 23:39
memberPhil Atkin2-Oct-06 23:39 
GeneralNeat little project Pin
Udnaan2-Oct-06 22:45
memberUdnaan2-Oct-06 22:45 
GeneralRe: Neat little project Pin
Matt Fichtenbaum3-Oct-06 6:56
memberMatt Fichtenbaum3-Oct-06 6:56 
GeneralGreat timing! Pin
CaveManJ2-Oct-06 18:53
memberCaveManJ2-Oct-06 18:53 
GeneralMost useful - but truncates some directory names Pin
RogerLainson2-Oct-06 18:19
memberRogerLainson2-Oct-06 18:19 
GeneralRe: Most useful - but truncates some directory names Pin
Matt Fichtenbaum3-Oct-06 5:50
memberMatt Fichtenbaum3-Oct-06 5:50 
GeneralIt's a very useful app for me Pin
raulcigil227-Sep-06 23:08
memberraulcigil227-Sep-06 23:08 
GeneralGreat! Pin
Anonymuos26-Sep-06 12:44
memberAnonymuos26-Sep-06 12:44 
GeneralRe: Great! Pin
Matt Fichtenbaum29-Sep-06 2:58
memberMatt Fichtenbaum29-Sep-06 2:58 

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.170217.1 | Last Updated 3 Oct 2006
Article Copyright 2006 by Matt Fichtenbaum
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid