469,362 Members | 2,287 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

Watching a folder

63
Can anyone give me some help, plz

I want to create a perl script to be watching a folder.
Everytime a new file gets there it triggers some code;

How can I do this?

TIA,

Joćo Correia
Sep 21 '07 #1
11 6270
KevinADC
4,059 Expert 2GB
For all I know there is a module already written for watching a folder. But you could open the folder and read the list of files into another file (files.txt). Every so often have a perl script open the folder and check the list of files against the list of files in files.txt. If there is a new file run your code.
Sep 21 '07 #2
jcor
63
I found this Module,
File::Monitor;
From what I read in CPAN I made this script:

Expand|Select|Wrap|Line Numbers
  1. #/usr/bin/perl
  2. use File::Monitor;
  3.  
  4. $Monitor = File::Monitor->new();
  5.  $monitor->watch({
  6.   name     => '/home/joćo/PERLFOLDER',
  7.   recurse  => 1,
  8.   callback => \&outputFunc, 
  9.   }
  10. );
  11.  
  12. $monitor ->scan() ;
  13.  
  14. for ($i=0; $i<10; $i++) {
  15.     $monitor->scan ();
  16.     sleep 10;
  17. }
  18.  
  19. sub outoutFunc {
  20.   $a=1;
  21.   while ( $a < 10 ) {
  22.     print '"This is my output \n";
  23.     $a +=1;
  24.   }
  25. }
  26.  
MY problems are:
With this script I only get my output when the program stops running. I'd like that in the moment that it find some difference in the folder to give me the output;
And i'd like the script to NOT stop running. My goal is to have it running in backgroud allways.

Thanks,

Joćo
Sep 24 '07 #3
numberwhun
3,503 Expert Mod 2GB
I found this Module,
File::Monitor;
From what I read in CPAN I made this script:

Expand|Select|Wrap|Line Numbers
  1. #/usr/bin/perl
  2. use File::Monitor;
  3.  
  4. $Monitor = File::Monitor->new();
  5.  $monitor->watch({
  6.   name     => '/home/joćo/PERLFOLDER',
  7.   recurse  => 1,
  8.   callback => \&outputFunc, 
  9.   }
  10. );
  11.  
  12. $monitor ->scan() ;
  13.  
  14. for ($i=0; $i<10; $i++) {
  15.     $monitor->scan ();
  16.     sleep 10;
  17. }
  18.  
  19. sub outoutFunc {
  20.   $a=1;
  21.   while ( $a < 10 ) {
  22.     print '"This is my output \n";
  23.     $a +=1;
  24.   }
  25. }
  26.  
MY problems are:
With this script I only get my output when the program stops running. I'd like that in the moment that it find some difference in the folder to give me the output;
And i'd like the script to NOT stop running. My goal is to have it running in backgroud allways.

Thanks,

Joćo
Having your script running 100% of the time would eat up some system resourece and you would have to ensure that they wouldn't be needed. Most of the time that I have seen a "folder monitoring" script, the script was put into a scheduler and run every minute to look for changes.

I am sure you could setup your scheduler to keep the script running or have the script continually loop somehow.

Sorry, at this point in my (early) day, that is all I can muster for input.

Regards,

Jeff
Sep 24 '07 #4
jcor
63
Well, I guess having it running every minute will work fine to me;

I'll try to find information about the scheduling you talked about;

Thanks for your help,

joćo
Sep 24 '07 #5
jcor
63
I'll try to use the crontab to make my script run every minute or something like that;
But now I have this problem. I need the input of my script to be the last time checked folder so I can see if there is any change.

How can I store my "folder" (the files there) and use it in the next scan?

thanks,

Joćo
Sep 24 '07 #6
numberwhun
3,503 Expert Mod 2GB
I'll try to use the crontab to make my script run every minute or something like that;
But now I have this problem. I need the input of my script to be the last time checked folder so I can see if there is any change.

How can I store my "folder" (the files there) and use it in the next scan?

thanks,

Joćo
You could always have a "temporary" filie in a directory and have only ONE copy of the file there. That way, your script would compare against it and if there is no difference, leave it. If there is, then over-write it and do what you need to do.

You probably won't be able to do anything live as the script isn't continuously running.

Just an idea.

Regards,

Jeff
Sep 24 '07 #7
KevinADC
4,059 Expert 2GB
If you use "strict" and declare your variables lexically you can run a perl script continuously without too much worry of memory leaks or system resources, but of course that has to be verified with some testing. To run a perl script continuously you can simply wrap it in a "while" block

while (1) {
some code
}


but the File::Monitor code you posted has at least one error and is not using "strict".
Sep 24 '07 #8
I think a different language like C# or VB.Net that integrates better with Windows (assuming you're using Windows) would be better. That way you could probably create a threaded program that uses some Windows API function that works like an event-handler when a folder is updated.

Are you on a windows platform with some C# experience?
Sep 24 '07 #9
numberwhun
3,503 Expert Mod 2GB
I think a different language like C# or VB.Net that integrates better with Windows (assuming you're using Windows) would be better. That way you could probably create a threaded program that uses some Windows API function that works like an event-handler when a folder is updated.

Are you on a windows platform with some C# experience?
Actually, from their code, the shebang line is a Unix path to their Perl binary and the OP mentioned "crontab" which is the Unix scheduling utility.

Regards,

Jeff
Sep 24 '07 #10
jcor
63
It's true, i'm using ubuntu 7.04
I've got a few diferences in my code now:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use File::Monitor;
  5. use File::Monitor::Object;
  6. use File::Monitor::Delta;
  7.  
  8. my $monitor = File::Monitor->new();
  9.  
  10. while (1){
  11.  
  12.  # Watch a directory
  13.    my  $delta=$monitor->watch( {
  14.         name        => '/home/joao/PERLFOLDER',
  15.         recurse     => 1,
  16.         callback    => \&outputFunc,
  17.   } 
  18.  
  19. );
  20.  
  21.     my @changes= $monitor->scan;
  22.  
  23.  for my $change (@changes){
  24.      if ($change->files_created()){
  25.         print "$change\n";
  26.  
  27.     }
  28.     }
  29. sub outputFunc {
  30.  
  31.     print "I'm in \n";
  32. }
  33.  
What I'd like this to do is to be allways watching the folder '/home/joao/PERLFOLDER'. Any time a new file is created there i'd like to work on it ( This is still far). I'm getting desperate here :-). Please somebody tell what can I to get what I want. I'm not even sure what Ishould put in the callback...

Thanks,

Joćo
Sep 25 '07 #11
numberwhun
3,503 Expert Mod 2GB
It's true, i'm using ubuntu 7.04
I've got a few diferences in my code now:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use File::Monitor;
  5. use File::Monitor::Object;
  6. use File::Monitor::Delta;
  7.  
  8. my $monitor = File::Monitor->new();
  9.  
  10. while (1){
  11.  
  12.  # Watch a directory
  13.    my  $delta=$monitor->watch( {
  14.         name        => '/home/joao/PERLFOLDER',
  15.         recurse     => 1,
  16.         callback    => \&outputFunc,
  17.   } 
  18.  
  19. );
  20.  
  21.     my @changes= $monitor->scan;
  22.  
  23.  for my $change (@changes){
  24.      if ($change->files_created()){
  25.         print "$change\n";
  26.  
  27.     }
  28.     }
  29. sub outputFunc {
  30.  
  31.     print "I'm in \n";
  32. }
  33.  
What I'd like this to do is to be allways watching the folder '/home/joao/PERLFOLDER'. Any time a new file is created there i'd like to work on it ( This is still far). I'm getting desperate here :-). Please somebody tell what can I to get what I want. I'm not even sure what Ishould put in the callback...

Thanks,

Joćo
I understand your desperation in wanting to make this work, but please know that your stress and deadlines to not translate into the same for us. We are here to help, but are not under the same constraints.

My suggestion to you would be to have the script do what you want it to do if it finds a new file and exit if it doesn't. Then, take that script and put it into the scheduler to run every minute. That way, you have something in place and working for you while you get the script running all the time.

Regards,

Jeff

P.S. - When posting code, please place code tags around the code in your future posts. I have cleaned up your postings thus far for you.
Sep 25 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by softwareengineer2006 | last post: by
4 posts views Thread by Dusan Micuch | last post: by
14 posts views Thread by Zed A. Shaw | last post: by
2 posts views Thread by George Ter-Saakov | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.