Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. vision_am_2015-08-04.mp3
  2. vision_am_2015-08-03.mp3
  3. vision_am_2015-07-31.mp3
  4. vision_am_2015-07-30.mp3
  5.  
  6. lum_pro_2015-08-04.mp3
  7. lum_pro_2015-08-03.mp3
  8. lum_pro_2015-08-01.mp3
  9. lum_pro_2015-07-31.mp3
  10. lum_pro_2015-07-30.mp3
  11. lum_pro_2015-07-29.mp3
  12. lum_pro_2015-07-28.mp3
  13. lum_pro_2015-07-27.mp3
  14.  
  15. my $num_to_keep = 2; # or get from @ARGV
  16. $num_to_keep = $num_to_keep - 1;
  17.  
  18. my $dir = "/home/mp3files";
  19. opendir my $DH, "$dir" or die "$! not open";
  20. my $dateRE = qr/d{4}-d{2}-d{2}/;
  21. my $fileRE = qr/^.+_$dateRE.mp3$/; # only mp3s
  22. my @files = sort grep {/$fileRE/ && -f "$dir/$_"} readdir($DH);
  23. close $DH;
  24.  
  25. my %hash = ();
  26. for $file (reverse @files) {
  27. my ($date) = $file =~ m/($dateRE)/;
  28. my ($fname) = $file =~ m/(.*)?_$dateRE/;
  29. push(@{ $hash{$fname} }, $file);
  30. }
  31.  
  32. for my $fname (sort keys %hash) {
  33. my @files = @{$hash{$fname}};
  34. print "nnnFILE: $fname<<n";
  35. for my $i (0..$#files) {
  36. if ($i > $num_to_keep) {
  37. unlink $dir/$files[$i];
  38. }else{
  39. print "ttttI will keep this file $files[$i]n";
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement