use strict; use warnings; use HTTP::Date; use File::stat; use Time::localtime; use Modern::Perl; use RTF::TEXT::Converter; my $fullpath = my $dir = 'C:\Policies\\'; my %hash; my %months = ( "Jan" => "1", "Feb" => "2", "Mar" => "3", "Apr" => "4", "May" => "5", "Jun" => "6", "Jul" => "7", "Aug" => "8", "Sep" => "9", "Oct" => "10", "Nov" => "11", "Dec" => "12" ); #print "full path is $fullpath \n"; opendir (DIR, $dir) or die $!; #print "dir is $dir \n"; while (my $dir = readdir(DIR)) { my $file = $fullpath = 'C:\Policies\\' . $dir; opendir (FILE, $file) or die $!; while (my $file = readdir(FILE)){ # print "7 reading $file\n"; if (($file =~/RTF/ || $file =~/rtf/ )&& $file !~/^~/ ){ my $path_file = $fullpath . '\\'. $file; #print '\nfullpath = ' . $fullpath . '\\' . $file . '\n'; my $string; my $object = RTF::TEXT::Converter->new(output => \$string); $object->parse_stream( $path_file ); # print ctime(stat($fullpath . '\\' . $file)->mtime); my ($wday, $mon, $day, $time, $yr ) = split (/\s+/, ctime(stat($fullpath . '\\' . $file)->mtime) ); # split on space # print "\n$wday, $mon, $day, $time, $yr \n"; # print "file is $file my date is $months{$mon}/$day/$yr\n\n" ; $hash{$dir}{$file}{UPLOAD_DATE} = $months{$mon} . "/" . $day . "/" .$yr; chomp $string; my @string = split("\n", $string); my $line_num =0; foreach my $line (@string) { $line_num ++; #print "Reading line number $line_num in $file\n"; chomp $line; if ($line =~ m/Date of Last Update:(.*)/){ my $date = $1; $date =~ s/^\s+//; my @last_update_date = split (/ /, $date); #print "Date of last update is: $last_update_date[0] line number $line_num\n"; $hash{$dir}{$file}{LAST_UPDATE_DATE} = $last_update_date[0]; } # print "5 line is $line \n"; if ($line =~ m/Document #:(.*)/){ # not needed because the file name is the policy number #print "Document number is $1 line number $line_num\n"; my $Policy_number = $1; $Policy_number =~ s/\t+/ /g; # replace all tabs with spaces $Policy_number =~ s/^\s+//; # remove leading spaces my @arr = split (/ /, $Policy_number ); $hash{$dir}{$file}{DOC_NUM_AND_VERSION} = $arr[0]; # print "array is @arr and element 0 is $arr[0]\n"; #print "line is $line \n"; #print "1Path is $fullpath " . "\\" . "$file\n"; } # Sign-Off Approvals if ($line =~/_____\/_____\/_____/){ #print $line; $hash{$dir}{$file}{NUM_OF_SIGS} ++; } } #print "$hash{$dir}{$file}{NUM_OF_SIGS}\n"; } } #print "dir is $dir\n"; } closedir(DIR); closedir(FILE); print_data (\%hash); sub print_data { my ($h_ref) = @_; open (OUT, '> C:\Dev\Policy_upload_dates.csv') or die ("Can't open the output file $!"); print OUT "Department,File_Name,Date_of_Last_Update,Upload_Date,Policy_Number,Number_of_Signatures\n"; foreach my $dir (keys %$h_ref){ foreach my $file (keys $h_ref->{$dir}){ printf OUT "%s,%s, %s,%s, %s,%s\n", $dir, $file, $h_ref->{$dir}{$file}{LAST_UPDATE_DATE}, $h_ref->{$dir}{$file}{UPLOAD_DATE}, $h_ref->{$dir}{$file}{DOC_NUM_AND_VERSION}, $h_ref->{$dir}{$file}{NUM_OF_SIGS} ; } } }