Guest User

Untitled

a guest
Oct 17th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.68 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. ######################################################################################
  3. # Author : Jan Roald Haugland janroald@gmail.com                                     #
  4. ######################################################################################
  5. # No effort is made to make this a good script :-)                                   #
  6. # Free to use, free to modify                                                        #
  7. # If something goes to hell; dont blame me. No guarantees provided...                #
  8. ######################################################################################
  9. ######################################################################################
  10. # Syntax :                                                                           #
  11. #   perl copyrandom.pl [source directory] [destination] ([max megabytes to transfer])#
  12. # Example :                                                                          #
  13. #   perl copyrandom.pl /home/you/mp3/ /media/ipod/ 500                               #
  14. ######################################################################################
  15. #500 is MB's you want to transfer (maximum). This is also default if u dont specify  #
  16. ######################################################################################
  17.  
  18. ######################################################################################
  19. # Config #############################################################################
  20.  
  21. # Add or remove extesions to be transferred here, separate with comma
  22. $extensions_allowed = 'mp3,ogg,wma,m4a';
  23.  
  24. # Edit number of megabytes to be transferred if not specified as argument
  25. $default_mb = 5000;
  26.  
  27. # Only edit below if u know what u are doing
  28. ######################################################################################
  29. ######################################################################################
  30.  
  31. # Parameters #########################################################################
  32.  
  33. $source = $ARGV[0];
  34. if($source !~ m/\/$/) { $source = $source.'/'; }
  35. $destination = $ARGV[1];
  36. $max_mb = (!$ARGV[2]?$default_mb:$ARGV[2]);
  37. $extensions_allowed =~ s/,/|/g;
  38.  
  39. # Other variables ####################################################################
  40. my $mb_total = 0;    #Start counting
  41. my $file = '';
  42. my @list = ();
  43. my @files = ();
  44. my %files_test = ();
  45. my $dir = '';
  46. ######################################################################################
  47.  
  48. print "\nLooking for songs in $source ...\n\n";
  49. print "Copying maximum ${max_mb}MB's to $destination...\n\n";
  50.  
  51. # Using shell 'ls' to list all files and directories
  52. @list = `ls -1pshR $source`;
  53. splice(@list, 0, 1);
  54.  
  55. # Looping through the list, selecting directories and songs
  56. # Generating songlist with trailing megabytes
  57. $x = 0;
  58. foreach(@list) {
  59.   if($_ =~ /^\.\/(.*):$/) {
  60.     $dir = "$1/";
  61.     $dir =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g;
  62.   }
  63.   if($_ =~ /^$source(.*):$/) {
  64.     $dir = "$1/";
  65.     $dir =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g;
  66.   }
  67.  
  68.   if( $_ =~ /^\s?([\d|\.]*)M\s((.*)\.($extensions_allowed))$/i ) {
  69.     $file = $2;
  70.     $mb = $1;
  71.     $file =~ s/([\s\(\)!&'"`\[\],\|:])/\\$1/g;
  72.     push(@files, "$dir$file|$mb");
  73.   }
  74. $x++;
  75. }
  76.  
  77. # Picking out random files and copying to destination. Making same song isn't copied twice
  78. $number_of_files = @files;
  79. while($max_mb > $mb_total) {
  80.   $random = rand($number_of_files);
  81.   ($song,$size) = split('\|',$files[$random]);
  82.   if(!$files_test{$song}) {
  83.     $mb_total+=$size;
  84.     $files_test{$song} = 1;
  85.     if($max_mb > $mb_total) {
  86.       `cp $source$song $destination`;
  87.       print "Transferred file $song  ${size}MB's ${mb_total}MB's total\n";
  88.     }
  89.   }
  90. }
Add Comment
Please, Sign In to add comment