use Win32::OLE;
use Win32::OLE::Enum;
use strict;

my $word = "C:/Users/Paul/Documents/Projects/Rutgers/Corpus/word/"; # directory in which doc/rtf input files live
my $text = "C:/Users/Paul/Documents/Projects/Rutgers/Corpus/textonly/"; # directory in which txt output files live
my $ext = ".txt"; # file extension to add to 'cleaned' files

print "Input Directory: $word\n";
print "Output Directory: $text\n";

my $name; # this is the name of a file in the $word directory

while (defined($name = <$word*>)) {
	my $output = $name; # get a copy of the filename, so we can manipulate it
	$output =~ s/$word//; # remove the file path from the filename
	
	if (($output =~ m/~/) or ($output !~ m/[.rtf|.doc]$/))
	{ next; } # if the file is a word backup file, or not an RTF/DOC, skip it
	
	print "$output --> "; # print a little information
	$output =~ s/\s+/_/g; # substitute underscores for (repeated) whitespace
	$output = $output . $ext; # add an appropriate extension
	print "$output"; # and a little more info
	$output = $text . $output; #  concatinate the filename onto the output directory

	if (-e $output)
	{ # if the output file already exists, don't bather reparsing the word doc
		print " ... exists\n";
		next;
	}
	print "\n";
	
	my $document = Win32::OLE -> GetObject($name); # load up the formatted document
	open (FH, ">$output"); # open up the output file

	my $paragraphs = $document->Paragraphs(); # grab a pointer to the paragraphs in the doc
	my $enumerate = new Win32::OLE::Enum($paragraphs); # get an enumerator of the paragraphs
	my $paragraph; # define a variable to hold a paragraph
	while(defined($paragraph = $enumerate->Next())) 
	{ #do this for each paragraph
		my $text = $paragraph->{Range}->{Text}; # grab the text of the paragraph
		$text =~ s/[\n\r]//g; # remove any LF/CR pairs
		$text =~ s/\x0b/\n/g; # swap line feeds for newlines
		print FH "$text\n"; # print the paragraph to the output file
	}

	close (FH); # close the output file
	#$document->Close();
	
}