Click here to Skip to main content
Click here to Skip to main content

Export Directory Data to XML

By , 22 Sep 2013
Rate this:
Please Sign up or sign in to vote.

Introduction

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;
try
{
	xtw = new XmlTextWriter(sw);
	xtw.Formatting = Formatting.Indented;
	doc.WriteTo(xtw);
}
finally
{
	if (xtw != null) xtw.Close();
}
txtXML.Text = sb.ToString(); 

History  

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.)

License

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

Comments and Discussions

 
GeneralGreat Tool PinmemberMauricio Kenny21-Sep-13 15:54 
GeneralMy vote of 5 Pinmemberfredatcodeproject21-Sep-13 11:46 
GeneralMy vote of 5 PinmemberCarsten V2.021-Sep-13 6:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 23 Sep 2013
Article Copyright 2013 by Chris Winkelmann
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid