zentara wrote:

On Sat, 12 Feb 2005 12:32:22 -0500, [EMAIL PROTECTED] (Brian Volk) wrote:



Hi All,
I have two directories, one directory contains 20 text files and the other directory contains 15 jpeg's. I need to create an html page for each text file and insert the image if one exist for that text file. BTW, the file names correspond to one another. For example, 12345.txt / 12345.jpg.


I'm pretty lost on how to go about this. My thinking so far was to




Here is a simple way. I like to do things manually, for absolute ease of understanding.

#!/usr/bin/perl
use warnings;
use strict;
#use Cwd;
use File::Basename;

umask 0022;
#my $dir = cwd;  #use this if you need full pathnames

#do a simple glob for .txt files my @files = <*.txt>;
print "@files\n";


#assume photos are in subdir  pics/*

foreach my $file (@files) {
my ($basename,$path,$suffix) = fileparse($file,'.txt');
open(HH, "> $basename.html") or warn "$!\n";
my $text = '';
open(TH, "< $file") or warn "$!\n";
read( TH, $text, -s TH );
close TH;


print HH<<EOH;
<html>
<BODY TEXT="#FFCCCC" BGCOLOR="#000000">
<center><h1>$basename</h1></center>
EOH

if( -e "pics/$basename.jpg"){
print HH<<EOI;
<center><IMG SRC="pics/$basename.jpg"  alt="[$basename.jpg]"></center>
EOI
}

print HH<<EOHTML;
<center>
<pre>
$text
</pre>
</center>
</body>
</html>
EOHTML

close HH;
}
__END__





Zentara,

Thank you so much... that worked perfect... I like the way you kept it simple for me.. well somewhat anyway.. :~) I do have ome more question if you don't mind.. If I had images w/o text... ( I know I said that wouldn't be the case ) but could I put an else statement after this bit of code so a html page would be created w/ just the image if there was not a corresponding text file? Something like...

if( -e "pics/$basename.jpg"){
print HH<<EOI;
<center><IMG SRC="pics/$basename.jpg" alt="[$basename.jpg]"></center>
EOI
} else { print HH<<EOI;
<center><IMG SRC="pics/$basename.jpg" alt="[$basename.jpg]"></center>
EOI


Or could I just take the "-e" out of the if statement? I'm gonna play around w/ it.. Once again, thank you so much for helping me learn!!! I promise to study this code untill I understand it fully.

Brian





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to