Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.65 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl -w
  2.  
  3. use File::Copy;
  4.  
  5. if(!$ARGV[0] || !-f $ARGV[0])
  6. {
  7.         print "error: need input file\n";
  8.         exit;
  9. }
  10.  
  11. if(!$ARGV[1] || !-d $ARGV[1])
  12. {
  13.         print "error: need dir to process\n";
  14.         exit;
  15. }
  16. $input_dir = $ARGV[1];
  17. $input_dir =~ s/\/$//;  # trim trailing /
  18.  
  19. # read dir to process
  20. opendir(DIR, "$input_dir") or die "can't open dir \"$input_dir\": $!";
  21. @dirlist = CORE::readdir(DIR);
  22. closedir DIR;
  23.  
  24. # open config file to process
  25. open(FILE, $ARGV[0]) or die "coudnt open input file\n";
  26. while(<FILE>)
  27. {
  28.         # make sure line is what we expect
  29.         if(/(.*?)\t+(.*?)(\s|\r|\n)*$/)
  30.         {
  31.                 $regexp = $1;
  32.                 $dest_dir = $2;
  33.  
  34.                 if($dest_dir =~ /^DELETE=(\d+)/)
  35.                 {
  36.                         $delage = $1;
  37.                 }
  38.                 elsif(!-d $dest_dir)
  39.                 {
  40.                         print "*** error: dir \"$dest_dir\" listed in cofig does not exist\n";
  41.                         exit;
  42.                 }
  43.  
  44.                 for my $file(@dirlist)
  45.                 {
  46.                         if(!-f "$input_dir/$file")
  47.                         {
  48.                                 # file has already been moved
  49.                         }
  50.                         else
  51.                         {
  52.                                 $f = "$input_dir/$file";
  53.                                 $fileage = int(-M $f);
  54.                                 if($f =~ /$regexp/i)
  55.                                 {
  56.                                         if($dest_dir =~ /^DELETE=(\d+)/)
  57.                                         {
  58.                                                 if($fileage > $delage)
  59.                                                 {
  60.                                                         print "> DELETING $f",
  61.                                                         " because file age $fileage is > delete age $delage\n";
  62.                                                         unlink($f);
  63.                                                 }
  64.                                         }
  65.                                         else
  66.                                         {
  67.                                                 print "> moving \"$f\" to \"$dest_dir\"\n";
  68.                                                 #move($f, $dest_dir)  or die "File cannot be copied.:\n***\n$!\n***\n";
  69.                                                 system("mv \"$f\" \"$dest_dir\"")
  70.                                         }
  71.                                 }
  72.                         }
  73.                 }
  74.         }
  75.         else
  76.         {
  77.                 next;
  78.         }
  79. }