Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > infinite loop to monitor directories

Reply
Thread Tools

infinite loop to monitor directories

 
 
mr.vlad.dracula@gmail.com
Guest
Posts: n/a
 
      05-13-2008
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. It's supposed to bypass
subdirectories, hidden files/directories as well.

Problem: if a file is there already on any of the directories, it will
print out the file just fine. However, if I delete that file and drop
another one, wait a minute or more, nothing displays. It's not really
going inside the directories after the first time.

Thank you...

#!/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){
find(\&process, @directories, no_chdir => 1);
}


sub process {

#only files, not directories. skip . and .. files

if (-f $_ and !/^\./){

if (-M $_ >= $MINUTES){

print "$File::Find::name\n";

}

}

sleep 1;

}
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-13-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:5ff50268-c5d2-41d3-93dd-
(E-Mail Removed):

> 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{HOME}/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__


--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
 
dracula
Guest
Posts: n/a
 
      05-13-2008
Thank you very much. I appreciate your inputs and suggestions.

/dracula


On May 13, 10:50 am, "A. Sinan Unur" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote in news:5ff50268-c5d2-41d3-93dd-
> (E-Mail Removed):
>
> > 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{HOME}/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__
>
> --
> A. Sinan Unur <(E-Mail Removed)>
> (remove .invalid and reverse each component for email address)
>
> comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/


 
Reply With Quote
 
dracula
Guest
Posts: n/a
 
      05-13-2008
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{HOME}/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>




On May 13, 11:18 am, dracula <(E-Mail Removed)> wrote:
> Thank you very much. I appreciate your inputs and suggestions.
>
> /dracula
>
> On May 13, 10:50 am, "A. Sinan Unur" <(E-Mail Removed)> wrote:
>
> > (E-Mail Removed) wrote in news:5ff50268-c5d2-41d3-93dd-
> > (E-Mail Removed):

>
> > > 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{HOME}/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__

>
> > --
> > A. Sinan Unur <(E-Mail Removed)>
> > (remove .invalid and reverse each component for email address)

>
> > comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/


 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-13-2008
dracula <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

[ Do *not* top-post and do *not* quote messages in full ]

> I read the documentation for file::find but still a little unclear.


The module is File::Find. Case matters.

> Would this be the only thing I need to skip subdirectories and hidden
> files/directories?
> find( { wanted => \&process, no_chdir => 1 }, @directories );


Of course not. What gave you that impression.

> If so, then I don't need this line then right?.
> return if not -f or /^\./;


You do need that line in sub process to skip things that are not plain
files or files whose names begin with a dot.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      05-13-2008

Quoth "A. Sinan Unur" <(E-Mail Removed)>:
>
> 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:
>

<snip>
> my $mtime = -M;
> my $age = $^T - $mtime * SECONDS_PER_DAY ;
>
> if ( time - $age > 60 ) {


....or forget both -M and $^T (especially since converting a time to a
float and back probably isn't a good idea) and just use

use File::stat;

if (time - stat($_)->mtime <= 60)

Ben

--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
(E-Mail Removed) Frank Herbert, 'Dune'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM
Multiple bin-directories with virtual directories? =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?= ASP .Net 0 11-09-2004 05:49 PM
How to map Project directories to Production sub-directories Joel Finkel ASP .Net 0 09-12-2003 06:47 PM
Using virtual directories for common directories (scripts, images, styles, etc.) Jeffry van de Vuurst ASP .Net 2 07-30-2003 07:00 PM



Advertisments