use strict; #sifts all the server logs based on media type my @servers=('XXXXX','YYYYYYY','ZZZZZZZ'); my $dir1='//workstation/share/directory/'; my @types= qw(mp3 avi mpg mpe wav mov rmj zip exe wma); foreach my $type (@types){ my $total=0; my $out = "$dir1/sifted/$type.txt"; open (OUT, "> $out") or die "Cannot write to '$out':$!"; foreach my $server (@servers){ my $in = "$dir1/$server\.txt"; unless(open IN,"< $in") { warn " Cannot read from '$in': $!"; next; } my $re = qr/\.$type\z/i; # I assume this is what you want? while () { chomp; if (/$re/){ # Get filesize my $kbytes = (stat)[7]/1024; if (defined($kbytes)) { $total += $kbytes; print OUT "$_\t$kbytes KB\n"; } else { print OUT "$_\tNOT FOUND\n"; } } } } my $mbytes = $total/1024; print OUT "\n\nTotal: $mbytes MB\n"; print "Finished $type...\n"; }