#!/usr/bin/perl -w # Library v0.2 # Organise your eBooks # Mabus # Started: 13/09/06 # This copy saved: 17/09/06 #If you're not me, change this to your own directory! my $directory = "/home/mabus/doc/library"; my $count = 0; opendir DIR, $directory or die "Couldn't open $directory :$!\n"; #interface to receive commands and call functions while(){ my @args = split; $_ = shift @args; &authmod($args[0], $args[1]) if ((/^authmod/i) and (@args == 2)); &dotform if (/^dotform$/i); &help if (/^help$/i); &remdate if (/^remdate$/i); &remdupe if (/^remdupe$/i); &replace($args[0], $args[1]) if ((/^replace/i) and (@args == 2)); &remauth if (/^remauth$/i); &term($args[0]) if ((/^term/i) and (@args == 1)); &version if (/^version$/i); } #Same as replace, but code changed to modify the first term sub authmod{ my $terminate = $_[0]; my $newterm = $_[1]; foreach $file (readdir DIR){ (my $converted = $file) =~ s/^$terminate\./.$newterm./g; if ($file ne $converted){ $count += 1; rename $file, $converted; } } print "Changed $count number of books labelled $terminate to $newterm.\n"; $count = 0; } #Puts books in a OReilly.Learning.Perl.Fourth.Edition.chm format sub dotform{ foreach $file (readdir DIR){ #replace commas, hyphens, ()s, colons, and spaces with periods (my $converted = $file) =~ s/[\(\)_, -:]/./g; #remove apostrophes $converted =~ s/\'//g; #remove duplicate periods $converted =~ s/\.{2,}/./g; #if anything but a period is the fourth to last character, put one in #this will make sure all files have extensions $converted =~ s/[^.](.{3})$/.$1/; #If the filename is going to be changed, change it if ($file ne $converted){ $count += 1; rename $file, $converted; } } print "$count filenames formatted.\n"; $count = 0; } #rather cluttered help message, I should write a readme sub help{ print "authmod\tLike replace, but must be the start of the filename.\n"; print "dotform\tConverts Me - Book One.pdf to Me.Book.One.pdf\n"; print "help\tThis menu\n"; print "remauth\tRemoves all of those author suffixes common in torrents\n"; print "remdate\tRemoves those mon.year extensions such as Apr.2006.pdf.\n"; print "remdupe\tNotifies of duplicate titles with differing extensions\n"; print "replace\tTakes two terms, replaces instances of first with second, if term is surrounded by dots\n"; print "term\tTakes a term as an argument, instances of term in your files is removed if surrounded by dots\n"; print "version\tPrints version info\n"; } #removes all of those nasty author tags... who needs that spam? sub remauth{ my @authors = qw/ eBook EEn iNTENSITY LiB DDU EITheLiSO BBL JGT SUN KB CAUDEX /; foreach (@authors){ &term($_) } } #so far we have a few different formats it removes, but basically it tries to take those timestamps off sub remdate{ foreach $file (readdir DIR){ #removes .Mon.200x. type stuff (my $converted = $file) =~ s/\..{3}.200.\.(.{3})$/.$1/; #removes 200x.ext type stuff $converted =~ s/200.\.(.{3})$/$1/; #removes Datecode.06062006 type stuff $converted =~ s/Datecode\.\d{8}\.(.{3})/$1/i; if ($file ne $converted){ $count += 1; rename $file, $converted; } } print "Removed $count dates.\n"; $count = 0; } #Lets you know if you have the same title with multiple extensions sub remdupe{ foreach $file (readdir DIR){ $file =~ s/.{3}$//; foreach $test (readdir DIR){ $test =~ s/.{3}$//; print $file . " has a duplicate.\n"; } } } #replaces a given term with a second given term, note that only entire sections between periods will work #such as blah.foo.bar, you can replace foo... but in foobar it will not replace foo #this is to prevent certain title manglings from accidentally happening #term has similar behaviour to this sub replace{ my $terminate = $_[0]; my $newterm = $_[1]; foreach $file (readdir DIR){ (my $converted = $file) =~ s/\.$terminate\./.$newterm./g; if ($file ne $converted){ $count += 1; rename $file, $converted; } } print "Replaced $count instances of $terminate with $newterm.\n"; $count = 0; } #removes a term given as an argument, case insensitive, if its preceded and followed by periods #see replace for info on what it replaces (basically, only replaces foo in xxx.foo.bar not foobar) sub term{ my $terminate = $_[0]; foreach $file (readdir DIR){ (my $converted = $file) =~ s/\.$terminate\.//g; if ($file ne $converted){ $count += 1; rename $file, $converted; } } print "Removed $count instances of $terminate.\n"; $count = 0; } #Well, pretty simple. You get it. sub version{ print "Library v0.2\n"; print "Author:\tmabus\n"; print "Email:\tmabus\@aculei.net\n"; print "Site:\thttp://aculei.net/~mabus\n"; }