#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 $VERSION by Jenda.Krynicky.cz (c) 2001\n" unless -d $_; } } else { while (1) { $path = BrowseForFolder "Watch directory for changes\nClick [Cancel] to finish directory selection" , CSIDL_DRIVES, BIF_RETURNONLYFSDIRS 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 FILE_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,$mon,$mday,$hour,$min,$sec; copy "$path\\$file" => "$path\\_b_a_c_k_u_p\\$file.$timestr"; $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;companyname=Jenda Krynicky;productname=watch_dirs;legaltrademarks=Jenda.Krynicky.cz;filedescription=Watch dirs for changes and make backup;legalcopyright=Jenda.Krynicky.cz" =cut