#!C:\Perl64\bin\perl.exe use Time::Local; # numfiles # looks through scans directories in a selected collection directory # counts up all files of input extension chosen # outputs txt file tab delimiting item number followed by number of files # # jody DeRidder, 8/17/09 ##Copyright (c) 2009, The University of Alabama Libraries. ## Contributed by Jody DeRidder, 8/17/09. ##All rights reserved. ##Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ## * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. ## * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the ## distribution. ## * Neither the name of The University of Alabama Libraries nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. ##THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ##THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ##CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ##PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ##LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, ##EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ×tamp; print $timestamp."\n"; $output = "C:\\Perl64\\output\\FileCountList".$timestamp.".rtf"; open (OUT, ">".$output) or die "can't open $output\n"; @bases = ("S:\\Digital\ Projects\\Digital_Coll_in_progress\\", "S:\\Digital\ Projects\\Digital_Coll_Complete\\"); print "Which directory do you want? Type the number and press enter:\n"; $basecount = scalar @bases; for ($i = 1; $i <= $basecount; $i ++){ print $i.") ".$bases[$i-1]."\n"; } $num = ; chop $num; $num --; $base = $bases[$num]; opendir(BASE, $base) or die "can't open $base\n"; while ($file = readdir(BASE)){ # print "looking at $file\n"; if ($file =~ /^\./){next;} # skip dot files $path = $base.$file; if (-d $path){ push (@dirs, $file); } } close(BASE); print "I'm testing $base. \n Here are the directories there. \n"; $dircount = scalar @dirs; for ($i = 1; $i <= $dircount; $i ++){ print $i.") ".$dirs[$i-1]."\n"; } #foreach (@dirs){ print "$_\n";} print "\nWhich directory do you want? \n Type the number\n"; print "AND if this is the destination directory, type ! then enter:\n"; $myval = 1; $num = ; chomp $num; if ($num =~ /^(\d+)\!/){ $num = $1; $myval = 2; # prevent the loop } $num --; $mybase = $base.$dirs[$num]."\\"; $thisdir = $mybase; while ($myval eq 1){ opendir(THIS, $thisdir) or die "can't open $thisdir\n"; undef @dirs; while ($file = readdir(THIS)){ if ($file =~ /^\./){next;} # skip dot files $path = $thisdir.$file; if (-d $path){ push (@dirs, $file); } } close(THIS); $dircount = scalar @dirs; if ($dircount == 0){ print "There are no directories below this. Shall I test files here?\n"; print "Y or N, and press enter:\n"; $answer = ; if ($answer =~ /n/i){ print "Bye Bye then!\n"; exit;} last;# break the loop } print "I'm testing $thisdir. \n Here are the directories there. \n"; for ($i = 1; $i <= $dircount; $i ++){ print $i.") ".$dirs[$i-1]."\n"; } print "\nWhich directory do you want? \n Type the number \n AND if this is the "; print "destination directory, type ! \n -- then press enter:\n"; $num = ; if ($num =~ /^(\d+)\!/){ $num = $1; $myval = 2; #break the loop } $num --; $thisdir.= $dirs[$num]."\\"; } print "Files will be tested in $thisdir\n"; print "\nDid we go down one directory too far?\n"; print "Y or N, and press enter\n"; $answer = ; if ($answer =~ /y/i){ $thisdir =~ s,^(.*\\).*?\\$,\1,; $mybase = $thisdir; } else{ $mybase = $thisdir;} print "Files will be tested in $mybase\n"; print "\nWhat extension should we test for? \nPlease enter without punctuation, and press enter:\n"; $ext = ; chomp($ext); # remove newline $answer = lc($ext); # lowercase it print "We will count only files ending in -->$ext<--\n"; print "Is this correct?\n"; print "Y or N, and press enter\n"; $answer = ; while ($answer =~ /n/i){ print "\nWhat extension should we test for? \nPlease enter without punctuation, and press enter:\n"; $ext = ; chomp($ext); # remove newline $answer = lc($ext); # lowercase it print "We will count only files ending in -->$ext<--\n"; print "Is this correct?\n"; } push (@mydirs, $thisdir); # start with given directory foreach $dir (@mydirs){ opendir(THIS, $dir) or warn "can't look through $dir\n"; while ($file = readdir (THIS)){ if ($file =~ /^\./ || $file =~ /Metadata/ || $file =~ /Admin/){ next;} # skip metadata, admin, and . files $path = $dir."\\".$file; if (-d $path){ #not working for some reason push (@mydirs, $path); # collect all other directories for further investigation } elsif ($file =~ /^([a-z]{1}[\d]{4}\_[\d]{7}\_[\d]{7}).*?\.$ext$/){ # define an item by letter followed by 4 numbers, underscore, 7 numbers, # underscore, 7 numbers -- and ending in selected extension $item = $1; if ($dir =~ /Transcripts/i){ # these need counting too, but the count should be kept separate from scans $transcr{$item} += 1; # add count based on item number } else{ $scans{$item} += 1;} } elsif ($file =~ /.*?\.$ext$/){ print OUT "WARNING: found another type of tiff: $file in $path\n"; } elsif ($file =~ /\.\w{2,3}/){ next;} # file with some other extension else{ print "what is this? $file at $dir\n";} } # ignores all other files close(THIS); } if (%transcr){ print OUT "\n\n********************* Transcripts counts *****************************\n\n"; @sorted = sort keys (%transcr); foreach $item (@sorted){ print OUT "$item\t".$transcr{$item}."\n"; } } if (%scans){ print OUT "\n\n********************* Scans counts *****************************\n\n"; @sorted = sort keys (%scans); foreach $item (@sorted){ print OUT "$item\t".$scans{$item}."\n"; } } close OUT; print "Results are in the $output file. Good bye!\n"; sleep(5); exit; sub timestamp{ print "hit enter twice please\n"; $date = `date`; $time = `time`; #print $date."\n"; if ($date =~ /.*? (\d*)\/(\d*)\/(\d*)/){ $date = $3.$1.$2; } #print $date."\n"; if ($time =~ /.*? (\d*)\:(\d*)\:(\d*)\./){ $time = $1.$2.$3; } #print $time."\n"; $timestamp = $date."T".$time; #print $timestamp."\n"; # following for unix #($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime($mydate); #$mon ++; #if ($mon < 10){ $mon="0".$mon;} #need 2 digits #if ($sec < 10){ $sec="0".$sec;} #if ($min < 10){ $min="0".$min;} #if ($hour < 10){ $hour="0".$hour;} #if ($mday < 10){ $mday="0".$mday;} #$year = $year + 1900; #$timestamp= "$year-$mon-$mday\T$hour:$min:$sec\Z"; }