Skip to content

Instantly share code, notes, and snippets.

@AndrewRussellHayes
Last active December 20, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewRussellHayes/6181438 to your computer and use it in GitHub Desktop.
Save AndrewRussellHayes/6181438 to your computer and use it in GitHub Desktop.
needed a quick script to monitor directory A and whenever files arrived there to copy to directory B and move to directory C. This is what I came up with. Very simple, dies if a directory is placed in A.
#!/usr/bin/perl
#copyMove.pl By: Andrew Hayes <http://AndrewHay.es/>
#Copyright (c) 2013 Andrew Hayes
#
# This takes a file from Dir A and copies it to Dir B and moves it to Dir C.
# When the copy suceedes it prints a line to the stdout
# When a copy fails it Dies. For now. Dies if dir placed in A.
#
use strict;
use warnings;
use File::Copy;
my $fromDir = '/Users/andrew/Home/Desktop/source files';
my $copyDir = '/Users/andrew/Home/Desktop/copy';
my $moveDir = '/Users/andrew/Home/Desktop/move';
my $file;
my $DIR;
for(;;){
opendir($DIR, $fromDir) || die "Error in opening dir $fromDir: $!";
while ($file = readdir($DIR)) {
next if $file =~ /^\./ || $file =~ /^Icon/; #ignore icon file and hidden files
print ("$file\n");
copy("$fromDir/$file","$copyDir/$file") || die "could not copy $copyDir/$file $!";
print("copied $fromDir/$file to $copyDir/$file\n");
move("$fromDir/$file","$moveDir/$file") || die "could not move $moveDir/$file $!";
print("moved $copyDir/$file to $moveDir/$file\n");
}
closedir($DIR);
sleep(5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment