A Perl script to arrange ogg files by their comment data
November 16, 2008
My music collection is organised perfectly, with not a single file missing artist, album, or track info. However, the underlying file structure is a mess, with symlinks pointing all over the place, and music files having horrible, lowercase filenames with gratuitous hyphens and underscores.
I searched long and hard for a way to fix this(surely someone else must have the same problem), but in vain. So, I did what any self-respecting computer geek would do — learned Perl!
After a couple of hours, I emerged with
use File::Path;
use File::Copy;
use Ogg::Vorbis::Header::PurePerl;
use Cwd;
$runDir = getcwd;
$musicDir = “$runDir/My Music”;
$startDir = “./”;
@directories = (“./”);
find(\&wanted, @directories);
sub wanted {
$fileName = $_;
$ext = substr($fileName,-4);
if($ext eq “.ogg”){
$ogg = Ogg::Vorbis::Header::PurePerl->new($fileName);
$title = ($ogg->comment(“title”))[0];
$artist = ($ogg->comment(“artist”))[0];
$album = ($ogg->comment(“album”))[0];
if($title eq “” || $artist eq “” || $album eq “”){
print “$fileName has missing data!\n“;
}
else{
$directory=“$musicDir/$artist/$album”;
$filePath=“$directory/$title.ogg”;
if(!(-d $directory)){
mkpath($directory);
}
print “Moving $fileName to $filePath\n“;
move(“$fileName”,$filePath);
}
}
}
Now I have a nice spick-n-span music directory. Yippee!