SPUG: mkfavhtm.pl -- Converting IE favorites to HTM (unix/mozilla users just ignore me)

John W. Krahn krahnj at telus.net
Mon Jul 10 13:26:46 PDT 2006


Aaron West wrote:
> Hardly spug-relevant, and half+ of you guys probably are mozilla users, but
> here goes...
>  
> Is your Internet Explorer Favorites list too full of junk? You *could*
> organize it, but I was too lazy for that. (I always liked Netscape bookmarks
> better, yet lately I'm an IE user.) 
> 
> I'm sure someone has a better solution, but here's mine; a script that reads
> all *.url files in the favorites folder and outputs HTML. When you are done
> you can del *.url (if you like the resulting page), or move or archive them
> (recommended). ZIP has some filename restrictions; I found WinRAR works.
> 
> Alright, I admit a hashtable isn't necessary. I could use a list, but a
> hashtable seemed easy, as long as you can remember the key element (comma)
> separator in hashtables is "\x1c".
> 
> The sort below is by modification date. Change to your liking, eg:
> 
> 1,$ s/$mtime, $url, $filename/$filename, $mtime, $url/
> 
> # mkfavhtm.pl - 7/09/2006 Aaron W. West tallpeak at hotmail.com
> # Usage: run inside the favorites directory, eg:
> # CD "%HOME%\Favorites"
> # perl mkfavhtm.pl > favorites.htm
> use strict;
> use HTTP::Date;
> print "<HTML><HEAD><TITLE>IE Favorites</TITLE></HEAD>\n";
> print "<BODY><H1>IE Favorites</H1>\n<UL>";
> my %url;
> while ( defined( my $filename = <*> ) ) {
>     if ($filename =~ /\.url$/i) 
>     {

You could always combine both of those:

while ( my $filename = <*.[uU][rR][lL]> ) {


> 	my (
> 	    $dev,  $ino,   $mode,  $nlink, $uid,     $gid, $rdev,
> 	    $size, $atime, $mtime, $ctime, $blksize, $blocks
> 	) = stat($filename);
> 	open FH, "<$filename" || die "Can't open file $filename";

Because of the high precedence of the '||' operator die() will never execute.
 You need to either use parentheses:

 	open( FH, "<$filename" ) || die "Can't open file $filename";

Or the low precedence 'or' operator:

 	open FH, "<$filename" or die "Can't open file $filename";

You should also include the $! or the $^E variable in the error message so you
know why the open failed.


It also may be more efficient to open the file first and then stat the opened
filehandle.

 	open FH, '<', $filename or die "Can't open file '$filename' $!";
        my $mtime = ( stat FH )[ 9 ];


> 	my $url = undef;
> 	while ( defined( my $line = <FH> ) ) {
> 	    if ( $line =~ /^URL=([^\r\n]*)/ ) {
> 		$url = $1;
> 	    }
> 	}
> 	close FH;
> 	$filename =~ s/\.url$//;

You forgot the /i option:

 	$filename =~ s/\.url$//i;


> 	$url{ $mtime, $url, $filename }++ 
> 	    if defined $url;
>     }
> }
> 
> for ( sort keys %url ) {
>     my ( $mtime, $url, $filename ) = split( "\x1c", $_ );

No need to remember the value of the $; variable, just use the $; variable
directly:

     my ( $mtime, $url, $filename ) = split $;;


>     printf qq{<LI><a href="%s">%s</a> - %s\n}, 
> 	$url, $filename, HTTP::Date::time2str($mtime);

No need for the extra overhead of printf:

     print qq{<LI><a href="$url">$filename</a> - }, HTTP::Date::time2str(
$mtime ), "\n";


> }
> print "</UL>\n</BODY>\n</HTML>\n";
> __END__


John
-- 
use Perl;
program
fulfillment


More information about the spug-list mailing list