|
Posted by dracula on May 13, 2008, 6:09 pm
Please log in for more thread options I read the documentation for file::find but still a little unclear.
Would this be the only thing I need to skip subdirectories and hidden
files/directories?
find( { wanted => \&process, no_chdir => 1 }, @directories );
If so, then I don't need this line then right?.
return if not -f or /^\./;
please advise and thanks in advance.
:)
<snip>
>
> > #!/usr/bin/perl
>
> > use warnings;
> > use strict;
> > use File::Find;
>
> > use constant SECONDS_PER_DAY => 24*60*60;
>
> > my @directories = ( "$ENV/Src/Test" );
>
> > while ( 1 ) {
> > find( { wanted => \&process, no_chdir => 1 }, @directories );
> > sleep 1;
>
> > }
>
> > sub process {
> > return if not -f or /^\./;
> > my $mtime = -M;
> > my $age = $^T - $mtime * SECONDS_PER_DAY ;
>
> > if ( time - $age > 60 ) {
> > print "$_ : $age\n";
> > }
>
> > }
<snip>
> Thank you very much. I appreciate your inputs and suggestions.
>
> /dracula
>
>
> > mr.vlad.drac...@gmail.com wrote in news:5ff50268-c5d2-41d3-93dd-
> > cf92ce7f0...@w4g2000prd.googlegroups.com:
>
> > > Do you think you can point me on the right direction?
>
> > > Desired result: Monitor directories indefinitely, print on screen if a
> > > file found is older than 1 minute.
>
> > ...
>
> > > #!/usr/bin/perl
>
> > > use warnings;
> > > use strict;
> > > use File::Find;
>
> > > my @directories = ("/home/foo/dir1","/home/foo/dir2");
> > > my $MINUTES = 0.0007; #<-1 min expressed in days
> > > my $condition = 1;
>
> > > #supposedly loops forever
> > > while ($condition == 1){
>
> > while ( 1 ) {
>
> > would get rid of the unnecessary variable $condition.
>
> > > find(\&process, @directories, no_chdir => 1);
> > > }
>
> > They syntax of your call is incorrect. From perldoc File::Find:
>
> > find
> > find(\&wanted, @directories);
> > find(\%options, @directories);
>
> > You would have noticed this if you had used some debugging print
> > statements:
>
> > Can't stat no_chdir: No such file or directory
> > Can't stat 1: No such file or directory
>
> > find( { wanted => \&process, no_chdir => 1 }, @directories );
>
> > > sub process {
> > > #only files, not directories. skip . and .. files
> > > if (-f $_ and !/^\./){
> > > if (-M $_ >= $MINUTES){
> > > print "$File::Find::name\n";
>
> > perldoc -f -X
>
> > -M Script start time minus file modification time, in days.
>
> > Now, the main problem. You have
>
> > > my $MINUTES = 0.0007; #<-1 min expressed in days
>
> > If you create a new file, -M on that file will return a negative number.
> > You have assigned to $MINUTES a positive number so the condition
>
> > -M >= $MINUTES
>
> > will never hold for any file created after the script has been started.
>
> > Because of this logic error, I am a bit confused about what you really
> > want. Assuming you mean what you say about files "older than one
> > minute", you need to compare to current time not to -M.
>
> > From perldoc perlvar:
>
> > $BASETIME
> > $^T The time at which the program began running, in seconds since
>
> > So, to find the files older than 60 seconds, you would use the
> > following:
>
> > #!/usr/bin/perl
>
> > use warnings;
> > use strict;
> > use File::Find;
>
> > use constant SECONDS_PER_DAY => 24*60*60;
>
> > my @directories = ( "$ENV/Src/Test" );
>
> > while ( 1 ) {
> > find( { wanted => \&process, no_chdir => 1 }, @directories );
> > sleep 1;
>
> > }
>
> > sub process {
> > return if not -f or /^\./;
> > my $mtime = -M;
> > my $age = $^T - $mtime * SECONDS_PER_DAY ;
>
> > if ( time - $age > 60 ) {
> > print "$_ : $age\n";
> > }
>
> > }
>
> > __END__
>
> > --
> > (remove .invalid and reverse each component for email address)
>
> > comp.lang.perl.misc guidelines on the
WWW:http://www.rehabitation.com/clpmisc/
|