Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- ######################################################################################
- # Author : Jan Roald Haugland janroald@gmail.com #
- ######################################################################################
- # No effort is made to make this a good script :-) #
- # Free to use, free to modify #
- # If something goes to hell; dont blame me. No guarantees provided... #
- ######################################################################################
- ######################################################################################
- # Syntax : #
- # perl copyrandom.pl [source directory] [destination] ([max megabytes to transfer])#
- # Example : #
- # perl copyrandom.pl /home/you/mp3/ /media/ipod/ 500 #
- ######################################################################################
- #500 is MB's you want to transfer (maximum). This is also default if u dont specify #
- ######################################################################################
- ######################################################################################
- # Config #############################################################################
- # Add or remove extesions to be transferred here, separate with comma
- $extensions_allowed = 'mp3,ogg,wma,m4a';
- # Edit number of megabytes to be transferred if not specified as argument
- $default_mb = 5000;
- # Only edit below if u know what u are doing
- ######################################################################################
- ######################################################################################
- # Parameters #########################################################################
- $source = $ARGV[0];
- if($source !~ m/\/$/) { $source = $source.'/'; }
- $destination = $ARGV[1];
- $max_mb = (!$ARGV[2]?$default_mb:$ARGV[2]);
- $extensions_allowed =~ s/,/|/g;
- # Other variables ####################################################################
- my $mb_total = 0; #Start counting
- my $file = '';
- my @list = ();
- my @files = ();
- my %files_test = ();
- my $dir = '';
- ######################################################################################
- print "\nLooking for songs in $source ...\n\n";
- print "Copying maximum ${max_mb}MB's to $destination...\n\n";
- # Using shell 'ls' to list all files and directories
- @list = `ls -1pshR $source`;
- splice(@list, 0, 1);
- # Looping through the list, selecting directories and songs
- # Generating songlist with trailing megabytes
- $x = 0;
- foreach(@list) {
- if($_ =~ /^\.\/(.*):$/) {
- $dir = "$1/";
- $dir =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g;
- }
- if($_ =~ /^$source(.*):$/) {
- $dir = "$1/";
- $dir =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g;
- }
- if( $_ =~ /^\s?([\d|\.]*)M\s((.*)\.($extensions_allowed))$/i ) {
- $file = $2;
- $mb = $1;
- $file =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g;
- push(@files, "$dir$file|$mb");
- }
- $x++;
- }
- # Picking out random files and copying to destination. Making same song isn't copied twice
- $number_of_files = @files;
- while($max_mb > $mb_total) {
- $random = rand($number_of_files);
- ($song,$size) = split('\|',$files[$random]);
- if(!$files_test{$song}) {
- $mb_total+=$size;
- $files_test{$song} = 1;
- if($max_mb > $mb_total) {
- `cp $source$song $destination`;
- print "Transferred file $song ${size}MB's ${mb_total}MB's total\n";
- }
- }
- }
Add Comment
Please, Sign In to add comment