#!/usr/bin/perl # # Another script from http://congoed.us # 2010-05-20 New sort to ignore preceding non-numbers # # All rights unreserved # Wellcome to use, modify and distrubute. # # copy .mp3 files to a directory with numbers as names # in the alfabetic sort order. # It also renames all filenames so files has only alfanumeric content. # it comes with 3 different sort routines check the sort # statement and uncomment the one You need. # Usage : perl cp_mp3z.txt Destination_to_copy_to # my $i =0; # Filenames start with 0000.mp3 my $num ; my $copy2; # Save the dest of files 1:st arg to program # # sub to use recursivly to handle nested directories # sub srename { local @list; # List of files local @slist; # Sorted list of files local $file; # Filname my $pos = shift; # Parameter to pick directoy chdir $pos ; opendir (DIRE,".") or die ( "Unable to open $pos"); @list = readdir (DIRE) or die ( "Unable to read $pos"); # @slist = sort @list ; # use alfabetic sort # @slist = sort {$b<=>$a} @list ; # Use numeric sort # sort numeric signed # @slist = sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] } @list; # @slist = sort { ($a =~ /(\d+)/)[0] cmp ($b =~ /(\d+)/)[0] } @list; # dir sort # @slist = @list; # sort ignore first 3 letters #@slist = sort { ($a =~ /...(\d+)/)[0] cmp ($b =~ /...(\d+)/)[0] } @list; # Sort numeric ignore preceding letters @slist = sort { ($a =~ /[^\d]*(\d+)/)[0] <=> ($b =~ /[^\d]*(\d+)/)[0] } @list; closedir DIRE or die ( "Unable to close $pos");; foreach $file ( @slist ) { if ( ( $file eq '.') ||( $file eq '..')) {next;} #Skip directory shortcuts # Substitute character I don't want in filenames $file=~ s/\'/\\\'/g ; $file=~ s/\&/\\\&/g ; $file=~ s/\$/\\\$/g ; $file=~ s/ /\\ /g ; $file=~ s/\(/\\\(/g ; $file=~ s/\)/\\\)/g ; # Is it a directory ? Walk down recursivly if ( -d $file ) { print "cd $file\n"; srename( $file) ; } else { # Otherwise copy the file to Your target $_=$file ; # But only if it's a mp3 You might want to change it if You use .ogg or .wma if ( /^.*\.mp3$/ ) { $num = sprintf("%04d.mp3",$i++); `cp $file $copy2/$num`; print "cp $file $copy2/$num\n"; } } } # end foreach print "cd ..\n"; chdir '..'; } # end sub #---------------main if ( $copy2 = shift ) { srename ('.'); } else { print " Supplie an argument to where the files should bee copied"; }