Forum Moderators: coopster & phranque

Message Too Old, No Replies

Script for listing "orphaned images"

Script inside.

         

theperlyking

3:03 pm on Apr 13, 2001 (gmt 0)

10+ Year Member



As mentioned in this thread [webmasterworld.com] here is my script for making a list of orphaned images, basically these are images that are no longer actually used on any pages and are just sitting round gathering dust in your images directory. Whilst this doesnt do the entire job, it gives you a helping hand.

Usage :
imagescan.pl HTML_Directory Images_directory

e.g
./imagescan.pl /usr/local/httpd/htdocs /usr/local/httpd/htdocs/images/


The script generates an HTML document with the images in named unused.html.

I may call myself the perly king for fun but I am just learning really so the script may need some tweaking to work perfectly for your needs, also I know I haven't necessarily done everything the best possible way. Feel free to share any ways you can see of improving it, if my memory serves me correctly there is at least one awful kludge buried in there :)
Feel free to use, modify and distribute this script.


#! /usr/bin/perl
#imagescan written By Darren Tarbard / www.tarbard.co.uk
if (! @ARGV){
print "Usage - imagescan.pl html_dir images_dir\n\n";
exit;
}
$wdir= @ARGV[0];
$compdir=@ARGV[1];
print "working directory=$wdir\n";
chdir($wdir) or die( "error couldnt change to directory $wdir.\n
Please check and try again\n ((($!)))");
@a = <*>;
$cnt=@a;
print "there are $cnt files in target directory\n";
foreach (@a) {
if (/(.html¦.shtml¦.txt)/)
{
push (@filelist,$_);
}
}
$numfiles=@filelist;
print "$numfiles files found of correct type\n";
print "inspecting files\n";
foreach(@filelist){
open FILE,$_;
print "===examining $_===\n";
@foo=<FILE>;
foreach(@foo){
s/"(.*?[.jpg¦.jpeg¦.gif])"(.*?)/-s-$1-daz222--e-/ig;
@spl=split (/(-s-)¦(-e-)/);
foreach(@spl){
if (m/-daz222-/ && m/.jpg¦.gif/){
s/-daz222-//;
push (@globlist,$_);
}
}
}
}
@globlist=sort(@globlist);
foreach (@globlist){
if ($_ ne $last){
push (@newgloblist,$_);
$last=$_;
}
}
print "\n now checking dest dir \n";
chdir($compdir) or die( "error couldnt change to directory $compdir.\n
Please check and try again\n ((($!)))");
@a = <*>;
$cnt=@a;
$cnt = $cnt;
print "there are $cnt files in comparison directory, creating list of unused image files\n\n";
foreach $a (@a){
$mf=0;
$a="images/$a";
foreach $g (@newgloblist){
if ($a eq $g) {$mf=1}
}
if ($mf!=1){
push (@compglob,$a);
}
}
$siz=@compglob;
$siz2=@newgloblist;
chdir ($wdir);
open (HTML,">>unused.html") or die("COULDNT OPEN HTML FILE");
print HTML "<HTML><HEAD><TITLE>Unused images</TITLE></HEAD><BODY>";
foreach (@compglob){
print HTML "<img src='$_' height=50 width=50>$_<br>\n";
}
print HTML "</body></html>";
print "Imagescan finished. view file unused.html for list of unmatched images\n";

Brett_Tabke

4:14 pm on Apr 16, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Thank you thank you thank you. I have needed a script just like that for ages. I have a very specific need for it on a site that has 500 icons roaming around and I've moved the graphics left and right. With a little tweaking I can see that your script could be made to work in reverse for both finding orphaned images as well as orphaned img tags.

theperlyking

5:10 pm on Apr 16, 2001 (gmt 0)

10+ Year Member



:)

sugarkane

7:50 pm on Apr 18, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice. I particularly like the directory reading code - much simpler than the hideous, convoluted way I've been doing it :)