############################################################# # This script checks the validity of image files using # # Image Magick's Identify command. This script works with # # various file formats. Several variables can be changed to # # altering logging and analysis. # # # # Simon D. Yates 30may02. # # Simon D. Yates Version 2.00.0168 18jun02. # ############################################################# # Allow this for emailing on errors. Will also require the email method # Email me at sdyates2001@yahoo.com for the method. # use LWP::UserAgent use File::Find; use Win32::Process; #>---------------[ Customisable variables ]----------------- my $SummaryHTML = ">c:\\sdy\\summary.html"; # Location of Summary.html. Report summary in html format. my $SummaryCSV = ">c:\\sdy\\summary.csv"; # Location of Summary.csv. Report summary in csv format. my $SummaryCSVread = "----------------------[ variables ]------------------------ my @Structure=('\#\#','Format','Geometry','Class','Type','Depth', 'Colors','Filesize','Compression','Tainted'); #>---------------------[ Main logic ]------------------------ # Generates time stamp for log files Date(); # prompts for Source directory. RetrieveValues(); # Analyse each image and dump raw data and log errors CheckImageArchieve(); # Create summary file of images processed GrepImageDetails(); print OUTPUT "\n"; # Creates HTML file from CSV file PrintHTML(); #>----------------[ Subroutine Section ] -------------------- sub RetrieveValues { if ($AutoScript eq "no") { print "\n\nSpecify the Directory path to image folder:\n"; $directory = (); chomp $directory; } } sub CheckImageArchieve { my $a = 0; if ($AutoScript = "yes") { $directory=$CopySource } opendir(DIR, $directory) or die "Cannot open directory $directory"; find (\&RetrieveAll, $directory); system ("cls"); while ($DIR[$a]) { open (STDERR, "$error") or die "$!"; # verbose option is critical for creating summary file $file="$DIR[$a]"; if (-d $file) { system("echo ##: $DIR[$a](SUB DIRECTORY) >> $dump"); print "DIR: $file" . "\n" } elsif (-f $file) { system("echo ##: $DIR[$a] >> $dump"); print "$DIR[$a]\n"; $DIR[$a] =~ s/\\/\\\\/g; Win32::Process::Create($ProcessObj,"J:\\imagemagick\\win2k\\ImageMagick\\identify.exe","identify -verbose $DIR[$a] >> $dump",0,NORMAL_PRIORITY_CLASS,"."); $ProcessObj->Wait(INFINITE); $ProcessObj->GetExitCode( $ExitCode ); print "\nExit: $ExitCode\n"; if ($ExitCode ne "1") { print STDERR "##: $DIR[$a] likely corrupt!\n". "--------------------------------------------------\n"; $img="bad"; } else { $img=(); } CopyGoodImage(); } $a=$a+1; close STDERR; } } sub GrepImageDetails { open (INFILE, "<$dump") or die "Cannot open $dump file"; open (OUTPUT, $SummaryCSV) or die "Cannot open Summary CSVfile"; print OUTPUT "\n$Date\n"; print OUTPUT "File,Format,Geometry,Class,Type,Depth,Colors,Filesize,Compression,Tainted"; while () { $SFS = $_; $z=0; foreach $Structure(@Structure) { if ($SFS =~ m/$Structure[$z]/) { if ($Structure[1] =~ m/Format/) { if ($SFS =~ m/\#\#/) { print OUTPUT "\n"; } } $SFS =~ s/$Structure[$z]: //; $SFS =~ s/ //g; $SFS =~ s/\r//g; chomp $SFS; if ($z ne 9) { print OUTPUT $SFS . ","; } else { print OUTPUT $SFS; } print $SFS . "\n"; } $z=$z+1; } } $z=$z+1; close INFILE; close OUTPUT; } sub Date { ($sec,$min,$hou,$day,$mon,$yer,$wek,$dy,$DST)=localtime(time); my $rlm=$mon+1; my $yer=$yer+1900; if ($rlm == 1) {$rlm = "Jan"} elsif ($rlm == 2) {$rlm = "Feb"} elsif ($rlm == 3) {$rlm = "Mar"} elsif ($rlm == 4) {$rlm = "Apr"} elsif ($rlm == 5) {$rlm = "May"} elsif ($rlm == 6) {$rlm = "Jun"} elsif ($rlm == 7) {$rlm = "Jul"} elsif ($rlm == 8) {$rlm = "Aug"} elsif ($rlm == 9) {$rlm = "Sep"} elsif ($rlm == 10) {$rlm = "Oct"} elsif ($rlm == 11) {$rlm = "Nov"} elsif ($rlm == 12) {$rlm = "Dec"}; $Date="$hou"."h"."$min:$sec $rlm-$day-$yer"; } sub RetrieveAll { $File::Find::dir =~ s/\//\\/g; $_ =~ s/\//\\/g; push (@DIR,$File::Find::dir."\\".$_); } sub SummaryHTML { print OUTPUTHTML "

"; } sub PrintHTML { open (OUTPUTHTML, $SummaryHTML) or die "Cannot open Summary HTML file"; open (SUMMARY, "$SummaryCSVread") or die "Cannot open $dump file"; print OUTPUTHTML " "; print OUTPUTHTML "\n"; } } close SUMMARY; close OUTPUTHTML; } sub CopyGoodImage { if ($img eq "bad") { print "$file is potentially damaged.\nFile not coppied!\n\n"; $img=(); } else { print "$file is fine\n"; if ($CopyGoodImages eq "no") { print "File copy is turned off\n\n"; $img=(); } else { print "Copying $file ...\n"; system ("copy $file $CopyDestination\n"); } print "\n"; $img=(); } }
File name Format Pixel size Class Depth Colour Depth Colours File size Compression Tainted
"; $directory=~ s/\\/\\\\/g; while () { $line=$_; $line =~ s/,/\<\/td\>\n\/g; # replace with scalar representing the path if ($line =~ m/$directory/) { print OUTPUTHTML "
$line