#!/usr/bin/perl # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License, Version 1.0 only # (the "License"). You may not use this file except in compliance # with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # ident "@(#)mymp3sorter 1.3 07/05/15 fintanr@sun.com" # use MP3::Tag; use Getopt::Std; use File::Copy; use File::Basename; use strict; use vars qw/$opt_i $opt_o $opt_h $opt_v/; my $emusicDir = "/home/fintanr/Desktop/eMusic-Downloads"; my $outDir = "/tank/mp3"; my @mp3s = (); getopts("i:o:vh"); $opt_i ? $emusicDir = $opt_i : 1; $opt_o ? $emusicDir = $opt_o : 1; if ( defined($opt_h) ) { usage(); exit(); } listMp3s($emusicDir); sortMp3s(); sub sortMp3s { foreach my $file ( @mp3s ) { my $mp3 = MP3::Tag->new("$emusicDir/$file") || die("Can't read mp3 tags from $emusicDir/$file"); my $dir = crDir($mp3->autoinfo()); $opt_v ? printf("Moving %s/%s to %s\n", $emusicDir. $file, $dir) : 1; move("$emusicDir/$file", "$dir/$file") || die("Can't move $emusicDir/$file to $dir/$file"); } } sub crDir { my ( $dis, $dis, $artist, $album, $dis, $dis, $dis ) = @_; # we assume that people have a valid basedir, if they don't # they should probably create it themselves anyway. if ( ! -d "$outDir/$artist" ) { mkdir("$outDir/$artist") || die("Can't create $outDir/$artist"); $opt_v ? printf("Creating %s/%s\n", $outDir. $artist) : 1; } if ( ! -d "$outDir/$artist/$album" ) { mkdir("$outDir/$artist/$album") || die("Can't create $outDir/$artist/$album"); $opt_v ? printf("Creating %s/%s/%s\n", $outDir. $artist, $album) : 1; } return("$outDir/$artist/$album"); } sub listMp3s { my ( $inDir ) = @_; opendir(DIR, $inDir) || die("Can't open $inDir\n"); @mp3s = grep(/\.mp3/, readdir(DIR)); closedir(DIR); } sub usage { my $self = basename(__FILE__); printf("\nUsage : %s [-i input dir][-o output dir][-v][-h]\n", $self); printf("Where : -i directory containing the mp3s you wish to sort\n"); printf(" -d directory to place sorted mp3s and directory structure\n"); printf(" -v verbose output\n"); printf(" -h display this help\n\n"); }