using System;
using System.IO;
namespace fs2xml
{
class entry_point
{
// XML document object for writing out enumerated file system info
static protected System.Xml.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 "directory"
xml_writer = new System.Xml.XmlTextWriter(xml_file, System.Text.Encoding.UTF8);
xml_writer.Formatting = System.Xml.Formatting.Indented;
xml_writer.WriteStartDocument();
xml_writer.WriteStartElement("directory");
// 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
print_file_list(dir_name);
// list the subdirs
print_sub_dir_list(dir_name);
}
// get file info array
static void print_file_list(string dir_name)
{
DirectoryInfo root_dir = new DirectoryInfo(dir_name);
FileInfo[] files;
try
{
files = root_dir.GetFiles("*.*");
for(int i = 0; i < files.Length; i++)
{
xml_writer.WriteStartElement("file");
try
{
xml_writer.WriteElementString("name", dir_name + files[i].Name);
xml_writer.WriteElementString("last_mod", files[i].LastWriteTime.ToString());
Console.WriteLine(dir_name + files[i].Name);
}
catch{}
xml_writer.WriteEndElement();
}
}
catch
{
}
}
// get subdir info array
static void print_sub_dir_list(string dir_name)
{
DirectoryInfo root_dir = new DirectoryInfo(dir_name);
DirectoryInfo[] sub_dirs;
try
{
sub_dirs = root_dir.GetDirectories("*.*");
for(int i = 0; i < sub_dirs.Length; i++)
{
xml_writer.WriteStartElement("directory");
try
{
xml_writer.WriteElementString("name", dir_name + sub_dirs[i].Name);
xml_writer.WriteElementString("last_mod", sub_dirs[i].LastWriteTime.ToString());
Console.WriteLine(dir_name + sub_dirs[i].Name);
}
catch
{
}
// recursively print dir list
dir_list_to_xml(dir_name + sub_dirs[i].Name);
xml_writer.WriteEndElement();
}
}
catch
{
}
}
}
}