HtH### original code by Jenda as found at http://www.perlmonks.org/?node_ +id=105128 #perl -w # watch_dirs.pl use strict; #use G; use File::Copy; use Win32::IPC qw(wait_any); use Win32::ChangeNotify; use Win32::AbsPath qw(Relative2Absolute); use Win32::FileOp qw(:DIALOGS); use vars qw($VERSION $path @paths $i @notify @data $file $pathnum); $VERSION = "1.0"; if (@ARGV) { @paths = @ARGV; Relative2Absolute @paths; foreach (@paths) { die "Ussage: watch_dirs.pl [directory ...]\n\t\tversion $VERSI +ON by Jenda.Krynicky.cz (c) 2001\n" unless -d $_; } } else { while (1) { $path = BrowseForFolder "Watch directory for changes\nClick [C +ancel] to finish directory selection" , CSIDL_DRIVES, BIF_RETURNONLYF +SDIRS or last; push @paths, $path; } die "OK. No directory selected. I'm quiting.\n" unless @paths; } print "Watching directories:\n"; for ($i=0;$i<=$#paths;$i++) { $path = $paths[$i]; print "\t$path\n"; mkdir $path.'\\_b_a_c_k_u_p', 0777; opendir DIR, $path; while ($file = readdir DIR) { next if -d $path.'\\'.$file; $data[$i]->{$file} = (stat($path.'\\'.$file))[9]; } closedir DIR; push @notify, Win32::ChangeNotify->new($path,0,"LAST_WRITE SIZE FI +LE_NAME"); }; print "\n"; while (1) { $pathnum = wait_any(@notify) or die "Can't monitor the directory: +$!\n"; $pathnum--; # arrays always start from zero not one my $pathdata = $data[$pathnum]; my $path = $paths[$pathnum]; opendir DIR, $path; while ($file = readdir DIR) { next if -d "$path\\$file"; my $time = (stat("$path\\$file"))[9]; if ($pathdata->{$file} != $time) { print "$path\\$file has changed!\n"; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);$mon++;$year += 1900; my $timestr = sprintf '%04d%02d%02d-%02d%02d%02d',$year,$m +on,$mday,$hour,$min,$sec; copy "$path\\$file" => "$path\\_b_a_c_k_u_p\\$file.$timest +r"; $pathdata->{$file} = $time; } } closedir DIR; $notify[$pathnum]->reset; } =pod PerlApp perlapp watch_dirs.pl -f -c -x "-info=fileversion=1.0;productversion=1 +.0;internalname=watch_dirs.pl 1.0;originalfilename=watch_dirs.exe;com +panyname=Jenda Krynicky;productname=watch_dirs;legaltrademarks=Jenda. +Krynicky.cz;filedescription=Watch dirs for changes and make backup;le +galcopyright=Jenda.Krynicky.cz" =cut
In reply to Re: Monitor directory for new files and copy only new files to another directory
by Discipulus
in thread Monitor directory for new files and copy only new files to another directory
by JasonVoorheez
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |