here is some perl that definitely needs critiquing.
it renames and moves an .mp3 based on the value of the id3tag.
i will put it at the end of this message
sorry if this is off topic for this group.
thanks
randy
---perl script at end of message ---
> While we're critiquing his perl, let's beat him about the head for using
> an O(n^m) algorithm, which scales by the product of the file list and
> the search tree. He would be wise to stuff the files to notice into a
> hash as the keys. Then the check is pretty much O(1) and the the loop
> as a whole O(n). Eg:
>
> for my $item (@control_list)
> { $hash{$item}=1;
> }
>
> for my $file (@files)
> { chomp($file); # this should have happened when they were
> # read in but there you go ...
> if (exists $hash{$file})
> { print "$file\n";
> }
> }
>
> Gads! Do they teach programmers nothing these days?
> --
> Cameron Simpson, DoD#743 [EMAIL PROTECTED]
http://www.zip.com.au/~cs/
>
here is my perl script,my first one!
use absolute paths when specifying files
#!/usr/bin/perl
use MP3::Tag;
use File::Basename;
use File::Copy;
use Getopt::Std;
getopts('dmpPsx');
# m is for move
# p is for print
# P is for print with the path
# d is for set outdir for duplicates to "duplicates"
# s is for update input string only, it isnt a file
# x is to automatically make the extension .mp3
if ($#ARGV == -1 ) {
&usage;
exit 1
}
$options = $opt_m + $opt_p + $opt_P;
if ($options == 0 ) {
&usage;
exit 1
}
# static settings
# this is the base output directory
$outpath = "/var/ftp/mp3/";
$dupdir = "duplicates";
$dupcheck = "0"; # this might not be necessary
for (@ARGV) {
# here is where the inputfile name is defined as fullnamein
$fullnamein = $_;
fileparse_set_fstype ();
($filenamein,$filepathin,$suffix) = fileparse("$_",'\.\w{3}');
if ($opt_x == 1) {
$suffix = ".mp3"
}
# here is where the id tag version is set as tagver
$tagver = "nothing";
unless ($opt_s == 1) {
next until -f $fullnamein;
$mp3 = MP3::Tag->new($fullnamein);
$mp3->getTags;
if (exists $mp3->{ID3v1}) { $tagver = "old" };
if (exists $mp3->{ID3v2}) { $tagver = "new" };
for ($tagver) {
if (/old/) {
$artist = $mp3->{ID3v1}->artist;
$title = $mp3->{ID3v1}->song;
}
elsif (/new/) {
($artist, $name2) = $mp3->{ID3v2}->getFrame(TPE1);
($title, $name3) = $mp3->{ID3v2}->getFrame(TIT2);
}
elsif (/nothing/) {
$artist = "none";
$title = "none";
}
}
# i was having trouble with too many open files when i ran
# this program with a lot of files on the command line.
# i think this will fix that or at least it didnt hurt
$mp3->close;
$_ = "${artist}-${title}" if $tagver ne nothing;
$_ = "id3_error".$$ if $_ eq "-";
$_ = "id3_error".$$ if $_ eq "";
$_ = $filenamein if $tagver eq nothing;
}
# here is where the name extracted from tag is set as extractname
$extractname = $_;
$_ = "\L$_\E"; # changes the entire string to lower case
s/[}{)('\"':?,!=]/_/g; # these characters are _ " is escaped
s/[\]\[]/_/g; # the brackets are escaped and _
s/[\s+]/_/g; # changes all the spaces into underscores
s!/!-!g;
s!\\!-!g;
s/'+'/_/g;
s/.duplicate/_/g; # if this has already been labeled as one remove it
s/.mp3$/_/g; # if they somehow got mp3 in the tag, get it out
s/-+/-/g; # these next 9 are duplicates
s/\.+/\./g;
s/\.+$//g;
s/_+/_/g;
s/_-_/-/g;
s/-_/-/g;
s/_-/-/g;
s/_$//g;
s/-$//g;
s/[_-]track[_-][\d]{1,2}/-/g;
s/-[0-9]{1,2}-/-/; # remove -0X-
s/^[0-2]{1,2}-(.*)/$1/; # remove stuff like 01- at beggining
s/&/_and_/g;
s/^ccr/creedence_clearwater_revival/g;
s/^ac[_-]dc/acdc/g;
s/^beatles_the/beatles/g;
s/\#/_number_/g;
#s/^various[_-]artists[_-](.*)/$1/;
#s/^various[_-](.*)/$1/;
#s/^soundtrack[_-](.*)/$1/;
s/^the[_-](.*)/$1/;
s/^[-_](.*)/$1/;
s/-+/-/g; # here is where they are duplicated
s/\.+/\./g;
s/\.+$//g;
s/_+/_/g;
s/_-_/-/g;
s/-_/-/g;
s/_-/-/g;
s/_$//g;
s/-$//g;
$_= "\u$_";
# here is where the modified name is set as newname
$newname = $_ ;
# here is where the output directory is set as outdir based on the newname
if ($tagver eq nothing) {
if (/-/) { $outdir = "not" }
else {$outdir = "bad"}
}
else {
if (/^-$/i) {$outdir = "bad"}
elsif ($_ !~ /-/i) {$outdir = "bad"}
elsif (/^[A-C]/i) {$outdir = "a-c"}
elsif (/^[D-F]/i) {$outdir = "d-f"}
elsif (/^[G-I]/i) {$outdir = "g-i"}
elsif (/^[J-L]/i) {$outdir = "j-l"}
elsif (/^[M-O]/i) {$outdir = "m-o"}
elsif (/^[P-R]/i) {$outdir = "p-r"}
elsif (/^[S-U]/i) {$outdir = "s-u"}
elsif (/^[V-X]/i) {$outdir = "v-x"}
elsif (/^[Y-Z]/i) {$outdir = "y-z"}
elsif (/^[\d]/i) {$outdir = "0-9"}
else {$outdir = "er2"}
}
# here is were the complete filepath out is set as filepathout
$filepathout = "$outpath"."$outdir".'/';
$filenameout = $newname;
$fullnameout = $filepathout . $filenameout . $suffix;
#print "mp3updater says this is fullname out first time $fullnameout\n";
if ( $fullnamein eq $fullnameout ) {
#print "this one is done $fullnameout\n";
next
}
$suffix = "\L$suffix";
$fullnameout = $filepathout . $filenameout . $suffix;
while ( -e $fullnameout ) {
#print "this one already exists $fullnameout\n";
$filenameout .= "+";
$fullnameout = $filepathout . $filenameout . $suffix;
if ( $opt_d == 1 ) {
#print " i made it to set dupcheck\n";
$dupcheck = "1";
}
}
if ( $dupcheck == 1 ) {
$outdir = $dupdir;
$filepathout = "$outpath"."$outdir".'/';
$fullnameout = $filepathout . $filenameout . $suffix;
#print "this is a dup heading towards dupdir $fullnameout\n";
while ( -e $fullnameout ) {
#print "this is a dup heading towards dupdir $fullnameout\n";
$filenameout .= "+";
$fullnameout = $filepathout . $filenameout . $suffix;
}
}
mkdir $filepathout,0777 unless ( -d $filepathout );
if ( $opt_m == 1 ) {
move ($fullnamein,$fullnameout);
chmod 0444,$fullnameout;
chown 500,500,$fullnameout;
}
if ( $opt_p == 1 ) {
print "$filenameout";
}
if ( $opt_P == 1 ) {
print $filepathout . $filenameout;
}
}
# here is the begining of the subs
sub usage {
print "\n" . "Proper usage is mp3updater [options] <filename(s)>\n" .
" or mp3updater -s <string>\n" .
"\n" . "these are the options available.\n" .
"\n" .
" -d if file is a duplicate, change outdir to \"duplicates\"\n" .
" -m move the renamed file to the proper output directory\n" .
" -p print the new filename (no path or extension)\n" .
" -P print the new filename (with path but no extension)\n" .
" -s string supplied is not a file,just rename it (use -p) (no -m)\n" .
" -x Automatically makes the file extension .mp3\n" .
"\n" .
" if you dont supply -m or -p or -P, it wont do anything\n" .
"\n";
}
sub debug {
print "------------------------------------------------\n";
print "option d is $opt_d\n";
print "option m is $opt_m\n";
print "option p is $opt_p\n";
print "option P is $opt_P\n";
print "option s is $opt_s\n";
print "option x is $opt_x\n";
print "this is the fullnamein $fullnamein\n";
print "this is the filenamein $filenamein\n";
print "this is the filepathin $filepathin\n";
print "this is the suffix $suffix\n";
print "this is the tagversion $tagver\n";
print "this is the artist $artist\n";
print "this is the title $title\n";
print "this is the extractname $extractname\n";
print "this is the newname $newname\n";
print "this is the outdir $outdir\n";
print "this is the filepathout $filepathout\n";
print "this is the filenameout $filenameout\n";
print "this is the fullnameout $fullnameout\n";
}
_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list