Automatic FTP upload when content changed in folder - perl script

This was rudimentary script needed to edit more, but found interesting .


#! perl -slw
use strict;
use Win32::ChangeNotify;
use threads;
#monitoring path.
my $path = 'c:\folder';

my $notify = Win32::ChangeNotify->new( $path, 0, 'FILE_NAME' );

my %last;
@last{ glob $path . '\*' } = ();

my $count = 0;
while( 1 ) {
next
unless $notify->wait( 10_000 ); # Check every 10 seconds
$notify->reset;
print $/, 'Something changed';
my @files = glob $path . '\*';
if( @files> scalar keys %last ) {
my %temp;
@temp{ @files } = ();
delete @temp{ keys %last };
#print for keys %temp;
my $k;
my $v;
while ( ($k,$v) = each %temp ) {
my $thr1 = threads->create(\&load, $k,$count);
}
}
else {
print "A non-deletion or creation change occured";
}
undef %last;
@last{ @files } = ();
$count = $count +1;
}

sub load {
my ($file,$count) = @_;
if(fileSize($file))
{
ftp__command_generator($file,$count);
}

}
#check the file is still copying
sub fileSize {
my($filename,$count) = @_;
while(1)
{
my $value;
$value = open(FILEx,$filename);
if ($value)
{
close(FILEx);
return 1;
}
else
{
sleep(10);
}
}
}

sub ftp__command_generator {
my($Changed_file,$count) = @_;
my $outfile = "file$count.dat";
open (FILE2,">$outfile");
print (FILE2 "user USERNAME");
print (FILE2 "PASSWORD");
print (FILE2 "bin");
print (FILE2 "put \"$Changed_file\"");
print (FILE2 "quit");
close(FILE2);
system("ftp -n -s:$outfile 192.168.0.1");
}

Comments

Popular posts from this blog

l2tpv3 configuration reference

mikrotik queue tree - Per connection queuing.

Decoding BGP Notification Error