Click here to Skip to main content
11,923,517 members (69,802 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


15 bookmarked

Export Directory Data to XML

, 22 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple program to list directories and store file data (name, size, create date, and modified date) in an XML document


I just wanted to list my movie files with their size to a text document of sorts so I can have a quick reference of what movies I have available while I travel. I've used command line batch files which are simple, but I wanted something that can be displayed nicely in a browser or Notepad++ and collapse directories. 

Program Features  

The program requires a "Source Directory" and "Destination File" to process a list of directories and files and to save the resulting XML file. There are several options labeled "Report Options", "Filter Options", and "Sort Options" which make the output a little more usable. 

Report Options

  • Recursive Listing - Adds all sub directories to the XML report
  • Display Drive Info - Adds details of the root drive such as total size, free space, and percent used
  • Display Directory Info - Adds roll up information such as total size of all files in directory and then total for all sub directories
  • Display File Info - Adds file size in KB, MB, and GB, also Create Date and Modified Date
  • Display Full Path - Writes the File Name which includes the full path rather than the file name only

Filter Options

  • File Size > than (Bytes, KB, MB, GB) - filters file result where files greater than specified size are reported
  • File Search Pattern - filters file result by whatever mask is specified
  • Omit Empty Directories - will exclude from the report any directory where there was not any single file that met criteria for reporting (really cleans up the output if using other filters)

Sort Options 

  • Sort on (File Name, File Size, Modified Date, and Created Date)
  • Sort Ascending / Descending

The following is the screen shot of the program:

Points of Interest

The sort functionality was implemented with examples of IComparer<DirectoryInfo> and delegates such as System.Comparison<T>. I prefer using delegates rather than creating lots of nested classes implementing IComparer. Writing a few public or private static methods using delegates seems a little more clean; but if this were a large project / namespace, perhaps I'd create a static class with all the sort delegates encapsulated to help keep the project clean.

It was interesting using XmlDocument class, but it does not provide the nice format one may want to see if the XML is viewed as plain text. The XmlTextWriter class allows us to apply a standard format to make the output look very nice. Take a look at the following:  

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xtw = null;
	xtw = new XmlTextWriter(sw);
	xtw.Formatting = Formatting.Indented;
	if (xtw != null) xtw.Close();
txtXML.Text = sb.ToString(); 


This is the first and final version of the program. I know it's not polished, I was only interested in making a quick tool and thought it was worth sharing since I have benefited from other CodeProject members in the past. (ok, I did update the source code by separating the directory listing and xml building logic from the form UI logic... nothing of substance changed.)


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


About the Author

Chris Winkelmann
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGreat Tool Pin
Mauricio Kenny21-Sep-13 16:54
memberMauricio Kenny21-Sep-13 16:54 
GeneralMy vote of 5 Pin
fredatcodeproject21-Sep-13 12:46
memberfredatcodeproject21-Sep-13 12:46 
GeneralMy vote of 5 Pin
Carsten V2.021-Sep-13 7:06
memberCarsten V2.021-Sep-13 7:06 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 23 Sep 2013
Article Copyright 2013 by Chris Winkelmann
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid