Click here to Skip to main content
15,886,091 members
Articles / Programming Languages / C#
Tip/Trick

Export Directory Data to XML

Rate me:
Please Sign up or sign in to vote.
4.83/5 (6 votes)
22 Sep 2013CPOL2 min read 20.4K   1.1K   14   5
Simple program to list directories and store file data (name, size, create date, and modified date) in an XML document

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:

Image 1

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:  

C#
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)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseFantastic Tool ! Very Useful ! Thank You ! Pin
Member 1480010014-May-20 2:23
Member 1480010014-May-20 2:23 
PraiseExcellent tool for archivists or project managers Pin
Member 1412342619-Jan-19 10:32
Member 1412342619-Jan-19 10:32 
GeneralGreat Tool Pin
Mauricio Kenny21-Sep-13 15:54
Mauricio Kenny21-Sep-13 15:54 
GeneralMy vote of 5 Pin
fredatcodeproject21-Sep-13 11:46
professionalfredatcodeproject21-Sep-13 11:46 
pretty cool
GeneralMy vote of 5 Pin
Carsten V2.021-Sep-13 6:06
Carsten V2.021-Sep-13 6: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.