473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 6681
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,509 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,509 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,509 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,509 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

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

Similar topics

7
by: Claire | last post by:
Im sat here watching task manager and the memory consumption of my application rising second by second. What tools are there out there for me to use to find where it's all going please? (I wish...
3
by: Luis Esteban Valencia Muńoz | last post by:
Hi everyone, I created a VB.NET application that looks at a folder in my local system from which I can read the file, enter data into the database and delete the file. I am using the filewatcher...
2
by: paul | last post by:
Watching connections to your computer. Can some one tell where to start. I want to write some code to be alerted when some one connect to my computer and browses/copies/changes files or...
2
by: Stu | last post by:
Hi, I have been asked to interface with an AIX system running a COBOL application. Not having done this before we settled on text files written to a drive to communicate between the two systems....
6
by: softwareengineer2006 | last post by:
Your Boss is Watching Your Emails!! Find out why and how you should encrypt your confidential emails before sending in my website http://tinyurl.com/z4u8m
4
by: Dusan Micuch | last post by:
Hi, What's best way for Watching my Packet TCP and UDP ? Socket ? Some external DLL ? What I need to use for build programs like this? I want measure data on specific or anyone port at Real Time....
14
by: Zed A. Shaw | last post by:
Hi Everyone, Just putting out an announcement that I've released a new version of Vellum numbered 0.16. This version should be ready for people to use as not much of the internal structure has...
2
by: George Ter-Saakov | last post by:
I have a folder in my .NET applications where I keep images... My problem is that if I create a subfolder there ASP.NET application restarts... any way to exclude that folder from being watch by...
3
by: canabatz | last post by:
Hi all i got a page that is refreshing data in a grid every 4 seconds and displaying results from the database and check if something as changed! my question is if there is 100 users that is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.