using System;
using System.IO; // console.writeline, file system enumeration, etc.
using System.Text; // encoding.utf8
using System.Xml; // xmltextwriter
namespace fs2xml
{
class entry_point
{
// XML document object for writing out enumerated file system info
static protected XmlTextWriter xml_writer;
[STAThread]
static int Main(string[] args)
{
// check to see we have a directory name, and an output XML file name
if(2 != args.Length)
{
Console.WriteLine("Usage:");
Console.WriteLine(" fs2xml directory_name output_xml_file_name");
return -1;
}
// scratch variables
String dir_name = args[0];
String xml_file = args[1];
// test that initial directory even exists
if(false == Directory.Exists(dir_name))
{
Console.WriteLine("Error: " + dir_name + " does not exist.");
return -2;
}
// create the XML document, start with a root element of "listing"
xml_writer = new XmlTextWriter(xml_file, Encoding.UTF8);
xml_writer.WriteStartDocument();
xml_writer.WriteStartElement("listing");
// recursively dump file and subdir listing to xml file
dir_list_to_xml(dir_name);
// cleanup
xml_writer.WriteEndElement();
xml_writer.WriteEndDocument();
xml_writer.Close();
return 0;
}
// list files in current directory, as well as subdirectories and their contents
static void dir_list_to_xml(String dir_name)
{
// sanity check / ease of use later down the road in concatenation ops
if('\\' != dir_name[dir_name.Length - 1])
dir_name += '\\';
// list the files
fs_item_info[] file_list = get_file_list(dir_name);
for(int i = 0; i < file_list.Length; i++)
{
xml_writer.WriteStartElement("file");
xml_writer.WriteElementString("name", file_list[i].name);
xml_writer.WriteElementString("last_mod", file_list[i].last_modified.ToString());
xml_writer.WriteEndElement();
}
// list the subdirs
fs_item_info[] dir_list = get_sub_dir_list(dir_name);
// recursively call this same function for each subdir found
for(int i = 0; i < dir_list.Length; i++)
{
xml_writer.WriteStartElement("directory");
xml_writer.WriteElementString("name", dir_list[i].name);
xml_writer.WriteElementString("last_mod", dir_list[i].last_modified.ToString());
dir_list_to_xml(dir_name + dir_list[i].name);
xml_writer.WriteEndElement();
}
}
// get file info array
static fs_item_info[] get_file_list(string dir_name)
{
DirectoryInfo root_dir = new DirectoryInfo(dir_name);
FileInfo[] files = root_dir.GetFiles("*.*");
fs_item_info[] fsi = new fs_item_info[files.Length];
for(int i = 0; i < files.Length; i++)
{
fsi[i].name = files[i].Name;
fsi[i].last_modified = files[i].LastWriteTime;
}
//return (fs_item_info[])fsi.Clone();
return fsi; // ?????
}
// get subdir info array
static fs_item_info[] get_sub_dir_list(string dir_name)
{
DirectoryInfo root_dir = new DirectoryInfo(dir_name);
DirectoryInfo[] sub_dirs = root_dir.GetDirectories("*.*");
fs_item_info[] fsi = new fs_item_info[sub_dirs.Length];
for(int i = 0; i < sub_dirs.Length; i++)
{
fsi[i].name = sub_dirs[i].Name;
fsi[i].last_modified = sub_dirs[i].LastWriteTime;
}
return fsi;
}
}
// all file/dir info one needs should go in here
class fs_item_info
{
public String name = "";
public DateTime last_modified = DateTime.MinValue;
}
}