I've been working on moving from Win2k to Linux and learning Perl
at the same time, so I thought I'd kill two birds with one stone and
write a Perl program to convert my IE bookmarks to Mozilla format.
The meat of the code is at the end - comments and suggestions are
more than welcome.
At any rate, the code works and I have my bookmarks in Mozilla.
However, I'm not completely happy with it. The output is too
closely tied to the input. I'd like to rewrite it so that the
output type could be specified - html, Mozilla, Galeon, etc.
I'm not sure where to start, though. I had thought of reading in
the favorites and storing it in a tree-type data structure, but
wasn't sure exactly how to do this or how to write a routine
to walk the tree. Any ideas or pointers to resources?
-Phil
code follows:
open(OUTFILE,">$outfile") ||
die "Couldn't open $outfile for writing: $!";
print_header(\*OUTFILE,$rootdir);
# Walk the directory tree and print the links
print_favs(\*OUTFILE,$rootdir);
print_footer(\*OUTFILE);
# recursive function to walk the directory tree underneath
# the directory passed in as a parameter. First the directory
# is opened, then a list of the files/folders is read into
# an array, and then the array is split into a list of files and
# a list of directories. html links are output for the files
# and the function is called again for each of the directories.
# Stopping condition is when a directory is reached that has only
# files, no sub-directories.
sub print_favs {
my $fh = $_[0]; # output file handle
my $dir = $_[1]; # current directory
my (@itemlist, @filelist, @dirlist);
opendir(CURDIR, $dir) || die "Couldn't open $dir: $!\n";
@itemlist = readdir(CURDIR);
chdir($dir) || die "Couldn't chdir to $dir: $!\n";
if ($dir ne $rootdir) {
print $fh "<dt><h3 id=\"NC:BookmarksRoot\#$foldernum\"\>$dir</h3>\n";
print $fh "<dl><p>\n";
}
$foldernum++;
foreach (@itemlist) {
# print $fh "Item: $_\n";
# Only process non-symbolic links
if (!(-l $_)) {
if ((-d $_) && ($_ !~ /\.\.?/)) {
@dirlist = (@dirlist, $_);
}
elsif ((-f $_) && ($_ =~ /\.url$/i)) {
@filelist = (@filelist, $_);
}
}
}
foreach (sort @filelist) {
print $fh "<dt>".getlink($_)."\n";
}
foreach (sort @dirlist) {
print_favs($fh,$_,$foldernum);
}
print $fh "</dl><p>\n";
chdir("..");
}
# takes the name of a windows internet shortcut file (from 'Favorites')
# opens the file and parses out the url it refers to. The function
# returns an html link with the url as the href and the name of
# the file (without the .url file extension) as the label
sub getlink {
my ($file, $line, $url, $name, $link);
$file = $_[0];
open(URLFILE,$file) || die "Can't open $file: $!";
while (defined($line=<URLFILE>) && ($line !~ /^URL=/)) {
}
if (defined($line)) {
chomp $line;
$url = substr($line,4);
$url =~ s/\cM//;
#print "URL: $url\n";
$name = $file;
$name =~ s/.+\///;
$name =~ s/\.url//;
#print "Name: $name\n";
$link = "<a href=\"".$url."\">$name</a>";
close(URLFILE);
return $link;
}
else {
return "";
}
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]