#! c:\perl\bin\perl.exe # File System Watcher # # usage: fsw.pl [directory] # defaults to c:\winnt\system32 # # Get Win32::AdvNotify # http://www.generation.net/~aminer/Perl/ # # Win32::AdvNotify docs # http://www.generation.net/~aminer/Perl/AdvNotify.html # # Copyright 2001 H. Carvey keydet89@yahoo.com use strict; use Win32::AdvNotify qw(INFINITE Yes No All %ActionName %EventName); # Global variable for holding error messages my $error; # Get the directory to be 'watched' my $dir = shift || "c:\\winnt\\system32"; $dir = $dir."\\" unless ($dir =~ m/\\$/); if (! -e $dir) { print "Could not find $dir.\n"; exit 1; } if (! -d $dir) { print "$dir is not a directory.\n"; exit 1; } $dir = $dir."\\" unless ($dir =~ m/\\$/); my $obj = new Win32::AdvNotify || die "Can't create object: $^E\n"; my $thr_1 = $obj->StartThread(Directory=>$dir, Filter => All, WatchSubtree => Yes) || die "Could not start thread.\n"; $thr_1->EnableWatch(); print "Watching directory ".$dir."...\n"; print "Events written to fsw.log.\n"; report("\n".localtime(time).": Monitoring ".$dir); while ($obj->Wait(INFINITE)) { my @data; while ($obj->Read(\@data)) { foreach my $i (0..$#data) { report(localtime(time).": ".$data[$i]->{Directory}.$data[$i]->{FileName}." ". $ActionName{$data[$i]->{Action}}); } } } print "The signal is: ".$EventName{$obj->{Event}}."\n"; $thr_1->Terminate(); $obj->Free; #---------------------------------------------------------- # report() #---------------------------------------------------------- sub report { open(LOG,">>fsw.log"); print LOG $_[0]."\n"; close(LOG); }