Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use File::Copy;
- if(!$ARGV[0] || !-f $ARGV[0])
- {
- print "error: need input file\n";
- exit;
- }
- if(!$ARGV[1] || !-d $ARGV[1])
- {
- print "error: need dir to process\n";
- exit;
- }
- $input_dir = $ARGV[1];
- $input_dir =~ s/\/$//; # trim trailing /
- # read dir to process
- opendir(DIR, "$input_dir") or die "can't open dir \"$input_dir\": $!";
- @dirlist = CORE::readdir(DIR);
- closedir DIR;
- # open config file to process
- open(FILE, $ARGV[0]) or die "coudnt open input file\n";
- while(<FILE>)
- {
- # make sure line is what we expect
- if(/(.*?)\t+(.*?)(\s|\r|\n)*$/)
- {
- $regexp = $1;
- $dest_dir = $2;
- if($dest_dir =~ /^DELETE=(\d+)/)
- {
- $delage = $1;
- }
- elsif(!-d $dest_dir)
- {
- print "*** error: dir \"$dest_dir\" listed in cofig does not exist\n";
- exit;
- }
- for my $file(@dirlist)
- {
- if(!-f "$input_dir/$file")
- {
- # file has already been moved
- }
- else
- {
- $f = "$input_dir/$file";
- $fileage = int(-M $f);
- if($f =~ /$regexp/i)
- {
- if($dest_dir =~ /^DELETE=(\d+)/)
- {
- if($fileage > $delage)
- {
- print "> DELETING $f",
- " because file age $fileage is > delete age $delage\n";
- unlink($f);
- }
- }
- else
- {
- print "> moving \"$f\" to \"$dest_dir\"\n";
- #move($f, $dest_dir) or die "File cannot be copied.:\n***\n$!\n***\n";
- system("mv \"$f\" \"$dest_dir\"")
- }
- }
- }
- }
- }
- else
- {
- next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment