Find and View All Images on a HD with Perl

I have a number of image files scattered all over my hard-drive (Windows). I wanted to create a list of them all, convert each filename into an IMG tag, and write them all to one massive HTML file. I did this with several recursive “for” commands and an AWK file to format the IMG tags.

That didn’t work out so well. There were so many that my browsers had difficulty processing them.

I rewrote the whole thing as a single Perl file that writes out a maximum of 100 IMG tage per HTML file before opening a new one. A link to the next file is provided in each, except for the last file.

(I note that I have a bug in my code … I do not write the closing BODY and HTML tags when the final file is processed. I’ll fix that, soon. )

Here’s the script I used:

# imagefind.pl
# Locate all image files on the HD and create a series of HTML files
# to allow viewing of them.
# Copyright (c) 2013 by Jim Lawless (jimbo@radiks.net)
# MIT / X11 license
# See: http://www.mailsend-online.com/license2013.php

use strict;
use warnings;

@ARGV = qw(.) unless @ARGV;
use File::Find;

my $file_count = 0;
my $entry_count=1;
my $fn="";

sub process_file {
   if($fn eq "") {
      $file_count++;
      $fn=">/j/perldev/htm/file_" . $file_count . ".htm";
      open (FIL,$fn )or die $!;
      print FIL "<html><head /><body>\n";
   }
   $entry_count++;
   if($entry_count>100) {
      $file_count++;
      $fn="/j/perldev/htm/file_" . $file_count . ".htm";      
      print FIL "<a href=\"" . $fn . "\" >" . $fn  . "<p>";
      print FIL "</body></html>\n";
      close(FIL);
      $entry_count=1;
      open FIL, (">" .$fn) or die $!;      
      print FIL "<html><head /><body>\n";
   }
   my ($fname)=@_;
   print FIL "<p>" . $fname . "<br><img src=\"" . $fname  . "\"><p>","\n";   
}

sub finder { 
   my $name=$File::Find::name;
   if(($name =~ /[jJ][pP][eE]?[gG]$/)||
      ($name =~ /[gG][iI][fF]$/)||
      ($name =~ /[bB][mM][pP]$/)||
      ($name =~ /[pP][nN][gG]$/))   {
         process_file($name);
   }   
}

find(\&finder, @ARGV);
close(FIL);


Note that the directory that I write to is \j\perldev\htm … a directory which I had created ahead of time.

Note that the regexes look only for JPG, JPEG, GIF, BMP, and PNG files. You may want to add support for other types of image files.

To execute the code, I ran:

imagefind.pl /

…so that it would search from the root folder. Then, I opened the file file_1.htm from the work folder in a browser.

I found a lot of interesting stuff that I had either forgotten or didn’t know I had. I found cached copies of documents I’ve scanned that I need to delete or secure.

I found a bunch of “album” covers from my Amazon music purchases. I found some book covers from my Kindle application.

I now have quite a few HTML files to go through in order to inventory everything on my HD.

Advertisements

About Jim Lawless

I've been programming computers for about 36 years ... 30 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, Nuts and Volts, and others.
This entry was posted in Programming and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s