dkaplowitz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm in a bit of a quandary as to how to implement this, I need to scan a _local_ folder on a win2k machine, if any files in that folder are older than 5 minutes, I need a file to be generated and sent to another directory. I have some ideas about how to sort the folders by date, vis:
#!/bin/perl -w use strict; chdir 'C:/devl/bin' or die "Arghh!!! $!"; opendir(ROOT, ".") or die "Arghh!!! $!"; my @sorted_files= map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ -M $_, $_] } readdir( ROOT); print join "\n", @sorted_files; exit;
but I'm not hip enough to really know how to use something like stat() and another function to say if mdate is 360 seconds older than the present moment, create this file in this directory Thanks for any clues. Dave
  • Comment on script for scanning folder and sending a warning if a file within it is older than 5 minutes
  • Download Code

Replies are listed 'Best First'.
Re: script for scanning folder and sending a warning if a file within it is older than 5 minutes
by Limbic~Region (Chancellor) on Mar 19, 2004 at 18:54 UTC
    dkaplowitz,
    You really don't need to sort the files.
    #!/usr/bin/perl use strict; use warnings; my $dir = $ARGV[1] || 'C:/devl/bin'; chdir $dir or die "Unable to change directories to $dir : $!"; my $now = time; my $threshold = $ARGV[0] || 300; opendir( DIR , '.' ) or die "Unable to read $dir : $!\n"; for ( readdir( DIR ) ) { if ( abs( $now - (stat($_))[9] ) > $threshold ) { Create_Warning_File(); last; # Unless you want 1 warning per file } } sub Create_Warning_File { # You did not give enough information to fill this in }
    Cheers - L~R
      Thanks L~R for the reply. The script only needs to create a single file (say in another local directory) with no contents...another program will be monitoring that directory for the existence of this file generated by the script. Something like "touch warning.txt" would work.
        For the sake of some closure, I have something unpretty that works:
        use strict; open OUTPUT, ">c:/temp/warning.txt"; my $mytime=(time); my ($File,$FileStat,$CallOutDir); chdir 'F:\Solimar_Work_Directories\XVTP\XVTP Cluster Feed' or die "Arg +hh!!! $!"; opendir(ROOT, ".") or die "Arghh!!! $!"; foreach $File (readdir(ROOT)) { next if $File=~/^\./; next if ($File eq "_Watched_Folder_._Active_"); next if ($File=~/\._Checking_File_Readiness_$/); next if (-d $File); if ($FileStat = (stat $File)[9]){ if ($FileStat <= ($mytime - 300)) { print "File older than 5min found. The file name is $File"; print OUTPUT "File older than 5min found. The file name is $Fi +le"; last; } } } close OUTPUT;
        I'm looking into adding some more functionality and flexibility to what's there. In the meantime it's doing what I need it to.
Re: script for scanning folder and sending a warning if a file within it is older than 5 minutes
by dkaplowitz (Novice) on Mar 19, 2004 at 18:49 UTC
    I did find some more hints here, I modified them slightly. This one finds a single file older than 20 minutes and prints a message to stdout:
    #!/bin/perl -w use strict; my $mytime=(time); my ($File,$FileStat,$CallOutDir); chdir 'C:/devl/bin' or die "Arghh!!! $!"; opendir(ROOT, ".") or die "Arghh!!! $!"; foreach $File (readdir(ROOT)) { next if $File=~/^\./; if ($FileStat = (stat $File)[9]){ if ($FileStat <= ($mytime - 1200)) { print "File older than 20min found. The file name is $File"; #send mail here.... last; } } }
    I'd like it to list all files older than nnn minutes, and I will have to figure out how to make this spit out a file rather than a msg. to stdout.