Rename HTML files with title tag
February 13, 2006 10:11 PM Subscribe
I have a large number of HTML files with numeric names (001.html, 2990.html, etc.). I would like to have each file renamed with text drawn from each file's HTML title tag.
I have searched high and low for a way to do this, but have not been successful. Anyone know of any scripts or apps that can do this?
I have searched high and low for a way to do this, but have not been successful. Anyone know of any scripts or apps that can do this?
Best answer: Because I like a challenge... here's a perl script to do it (tries to avoid shell meta characters and duplicates):
posted by sbutler at 11:00 PM on February 13, 2006
#!/usr/bin/perl -wuse strict;use File::Find;use File::Spec::Functions qw/rel2abs :DEFAULT/;use HTML::TreeBuilder;if (@ARGV != 2) { print STDERR "usage: ./html-rename <in dir> <out dir>\n"; exit 1;}my $in = rel2abs( shift );my $out = rel2abs( shift );find( \&wanted, $in );sub wanted { my ($fname, $tree, $title, $count); return unless /\.htm(l)?$/; $tree = HTML::TreeBuilder->new_from_file( $_ ); if ($title = $tree->look_down( '_tag', 'title' )) { $fname = $title->as_text . '.html'; $fname =~ s/[\|&`\$\*\?~]//g; } else { $fname = $_; } if (-e catfile( $out, $fname )) { print STDERR "DUPLICATE: $fname\n"; return; } link $_, catfile( $out, $fname ); $tree->delete;}
posted by sbutler at 11:00 PM on February 13, 2006
BASH oneliner:
for x in `ls *.html`; do foo=`grep '' $x`; foo2=`echo $foo|sed 's///'|sed 's# ##'`; mv $x $foo2.html; echo $foo2;done
Note that this has no error checking and will fail on pages where the title has funky characters like spaces in it.
posted by Kickstart70 at 11:56 AM on February 14, 2006
for x in `ls *.html`; do foo=`grep '
Note that this has no error checking and will fail on pages where the title has funky characters like spaces in it.
posted by Kickstart70 at 11:56 AM on February 14, 2006
Dammit. Metafilter ruined my code.
check here instead.
posted by Kickstart70 at 11:57 AM on February 14, 2006
check here instead.
posted by Kickstart70 at 11:57 AM on February 14, 2006
Response by poster: Thanks folks! Excellent help.
posted by tranquileye at 10:14 AM on February 16, 2006
posted by tranquileye at 10:14 AM on February 16, 2006
« Older Is giving head supposed to be arousing to the... | v-day wannabe sweetheart: go big? (or forget with... Newer »
This thread is closed to new comments.
posted by charmston at 10:18 PM on February 13, 2006