# Everything after a hash is a comment
# This perl script scans for all HTM or HTML files in or under the current directory
# and creates an XML file that records where the file is located. The file name is placed
# in a "file" element".
# The outer element of the XML file is "mother". 
# This version 2000-02-09, Chris Bradley

# NOTE: This is WIndows NT-specific. It uses a the DOS "dir" command to create a
# temporary file "temp.xml" that contains the list of HTM and HTML files.
# The temp.xml file is also used to contain the name of the current working directory.
# A UNIX solution would have to use another solution

# Get the current working directory (will be removed later from all input lines of 
# the directory listing)
system ("cd > temp.xml");
open (inputfile,  "temp.xml");
$a=<inputfile>;
close (inputfile);

$curdirnamelen = length ($a);
#print "Length of b is ", $curdirnamelen;

# Here's the DOS "dir" call that traverses the tree and stores into "temp.xml"
system ("dir /b/s *.htm* > temp.xml");

# Now open the file just created, and use it as input to create the new "mother" XML file
open (inputfile,  "temp.xml");

# Open the "mother" output file for WRITE operations, and call it "mother.xml" in the current directory:
open (outputfile, ">mother.xml");

# Start the mother document with the opening "mother" tag:
print outputfile "<mother>";

# Now scan the file that contains all the filenames, using a "while" loop:
while ($a=<inputfile>) 
{
  # The variable "$a" contains the current input line from temp.xml.
  chomp($a);  # removes the line feed at the end of the input line (technical detail)
  
# remove the current working directory from the path name
  $a = substr ($a, $curdirnamelen, length ($a)-1);
  
  # Put opening and closing "file" tags round the current line
  print outputfile "\n  <file>$a</file>"
};

# Now output the closing "mother" tag.
print outputfile "\n</mother>";

# Exit cleanly by closing any open files:
close (inputfile);
close (outputfile); 

# Congratulations, you're through !