in reply to Monitor directory for new files and copy only new files to another directory

"I currently have this bit of code which works fantastic other than I need it to ignore files that have already been copied from directory"

here is what i came up with:
use strict; use warnings; use diagnostics; use File::Copy::Recursive qw(dircopy rcopy); use File::Path qw(make_path); my $dirname = "$ENV{USERPROFILE}\\Dropped Box\\"; my $dirname2 = "C:\\Dropped Stuff\\"; my $sleep = 5; # how long to sleep for while (1) { opendir( DIR, $dirname ) || die "Error in opening dir $dirname\n"; foreach my $filename ( readdir(DIR) ) { if ( not -e $dirname2 . $filename ) { print "copying $dirname$filename to $dirname2$filename\n"; dircopy( $dirname . $filename, $dirname2 . $filename ); rcopy( $dirname . $filename, $dirname2 . $filename ); } else { print "files already exists so im skipping\n"; } } closedir(DIR); sleep $sleep; 1; }
this will check for if files exists and if it doesnt exists in destination, then copy. then it will also update files int he root copy to directory if they have changes, but not directories and sub directorie)
if you dont want to copy a file that has been copied, it would barely be trivial to setup a small condition to check a listfile that gets populated with filenames that have been copied. and add a check to see if the filename exists in the listfile, and if it does skip it :)

oh btw, this will copy any file that gets dropped in the directory. and does NOT do a backup, but that could be easily integrated ;)
  • Comment on Re: Monitor directory for new files and copy only new files to another directory
  • Download Code

Replies are listed 'Best First'.
Re^2: Monitor directory for new files and copy only new files to another directory
by james28909 (Deacon) on Oct 10, 2014 at 23:47 UTC
    i wonder how someone could set this up for directory recursion that would include all folders and subfolders?