Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

glob directory to array, then wait, and check again

by flieckster (Scribe)
on Aug 19, 2017 at 14:31 UTC ( [id://1197658]=perlquestion: print w/replies, xml ) Need Help??

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

hello, I'm wondering the best way to proceed. I'd like to check a directory for files, then wait for a period of time, then check the directory again and compare the first count to the second count. If it changed, sleep again, compare until the reading are equal.
#!/usr/bin/perl -w use File::Basename; use POSIX qw(strftime); my $time = strftime("%r",localtime); my $dir = "/Users/flieckb/Desktop/testing/2push/"; chdir( $dir ) or warn "$!"; (@dir_files) = glob "*"; my $dircount = @dir_files; if (@dir_files > 0) { print "$time there are @dir_files files\n"; sleep (10); (@dir_files2) = glob "*"; if (@dir_files != @dir_files2) { $equals = 0; print "$time there are @dir_files files\n"; print "$time there are @dir_files2 files\n"; } else { $equals = 1; print "$time there are @dir_files2 files\n"; } }

Replies are listed 'Best First'.
Re: glob directory to array, then wait, and check again
by Athanasius (Archbishop) on Aug 19, 2017 at 16:13 UTC

    Hello flieckster,

    You need to loop; for example:

    use strict; use warnings; use POSIX qw( strftime ); my $dir = $ARGV[0] // '.'; my $wait = $ARGV[1] // 10; chdir $dir or die "$!"; my $count = () = glob '*'; print_dir_status($count); if ($count > 0) { my $equal = 0; until ($equal) { sleep $wait; my $new_count = () = glob '*'; $equal = 1 if $new_count == $count; $count = $new_count; print_dir_status($count); } } sub print_dir_status { my ($count) = @_; my $time = strftime "%a %d %b %Y %I:%M:%S %p", localtime; print "$time: there are $count files\n"; }

    Update: Note that (contrary to the node title) it’s not necessary to store the results of the glob in an array. The syntax $count = () = glob '*'; evaluates glob in list context (via the assignment to empty parentheses), then implicitly puts that list into scalar context (by assigning to a scalar variable) to get the list count.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      thats works perfectly! thank you so uch.
Re: glob directory to array, then wait, and check again
by afoken (Chancellor) on Aug 19, 2017 at 19:04 UTC
    I'd like to check a directory for files, then wait for a period of time, then check the directory again and compare the first count to the second count.

    That sounds like you want to use dnotify or the newer inotify, or some very similar notification mechanism if you are not using Linux (kqueue on the *BSDs, FSEvents on MacOS X). This avoids inefficient polling in userspace, the kernel will wake up your program whenever something changed that you wanted to be notified of (e.g. create or remove files).

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: glob directory to array, then wait, and check again
by stevieb (Canon) on Aug 19, 2017 at 15:30 UTC

    Please edit your question and add a problem statement; ie. what the current behaviour (result) is, and what the desired behavour would be. Essentially, what is your code doing or not doing that you feel it should/shouldn't be.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1197658]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found