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

unable to monitor file size change with win32::changenotify

by somsub (Initiate)
on Jul 11, 2009 at 21:29 UTC ( #779257=perlquestion: print w/replies, xml ) Need Help??

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

I used the below perl code to check the change in file size :
1>use Win32::ChangeNotify ; 2>print " \n Enter a file path "; 3>$pt = <STDIN>; 4>chomp $pt; 5>$notify = Win32::ChangeNotify->new($pt, true ,"SIZE"); 6>unless ($notify->wait( '1000' )) 7>{ 8> print "Nothing Changed"; 9>} 10> 11>$notify->reset; 12>print 'Something changed';
after executing the above code (saved in fl1.pl) it asks for the path name (the path where the file is actually getting copied)including the file name . But after giving the file path (e.g F:\test1\ee.avi ) It throws error in command console "Can't call method "wait" on an undefined value at fl1.pl line 6, <STDIN> line 1." Could any one please help me how to modify this code.. or whats wrong with this.. so that it can monitor(periodicaly) .. the change in file size which is ( ee.avi sized 1.9 gb )getting downloaded . Advance thanks for any suggestions on this....

Replies are listed 'Best First'.
Re: unable to monitor file size change with win32::changenotify
by CountZero (Bishop) on Jul 11, 2009 at 21:55 UTC
    According to the documentation of this module, it can only monitor a directory and not an individual file. Also "true", should be 1 or another true value if you wish to monitor sub-directories too.

    Your $notify object is therefore not correctly initialised and has thus the value undef. It is best to check if the constructor worked by adding "or die 'Object construction failed';" to line 5.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: unable to monitor file size change with win32::changenotify
by Corion (Patriarch) on Jul 11, 2009 at 22:19 UTC
Re: unable to monitor file size change with win32::changenotify
by ramlight (Friar) on Jul 11, 2009 at 22:17 UTC
    The biggest problem is that you are trying to monitor a file; when I try to do that with this code, I get the same error that you do. If, however, you specify only a directory at the prompt in your program, you will get a little further.

    Of course when you get that far, you will discover that your program prints both "Nothing Changed" and "Something Changed" if nothing has changed. You might want to look into your logic a little further.

Re: unable to monitor file size change with win32::changenotify
by GrandFather (Saint) on Jul 11, 2009 at 23:42 UTC

    Following on from CountZero's advice: always use strictures (use strict; use warnings;). Both the issue with 'true' and the constructor failure would have been picked up under strictures.


    True laziness is hard work
      Thankx everybody for ur interest.... on this I modified my code like following ...
      use Win32::ChangeNotify; print " \n Enter a file path "; $pt = <STDIN>; chomp $pt; $notify = Win32::ChangeNotify->new($pt, true ,"SIZE"); $cnt = 0; while(1) { $cnt++; if ($cnt > 1) { print " \n File copy is going on "; $notify->reset; } $lmt--; next if ($notify->wait(10000)); print " \n The counter value is $cnt \n "; last; } print " \n hello world "; $notify->Close();
      after I set one file for copy if I execute the script it will only show the result as following: "The counter value is 1" "hello world ." That means it is not iterating through the loop after 1 st wait call because wait call unable to get signalled within 10000 milliseonds although hfe file copy still is going on.. . I think because while copying windows allocates size priorly in the destination as same size of source thats why although its being copied and getting increased in sized.. the $notify->wait(10000) call unable to detect the changes in filesize.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2022-10-02 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (7 votes). Check out past polls.

    Notices?