Gossamer Forum
Home : General : Perl Programming :

need HELP

Quote Reply
need HELP
hi

I need really HELP
this perl script opens and reads the info file,
renames the first file, sends via ftp,
waits 15 minutes, renames the second file and sends via ftp

info file looks:
----------------------------
XX10000022 VV000000011

YY1000022 ZZ0000000012
----------------------------

all files are TEXTfiles.
the problem is, it sends both files to the same directory.
how to change this script, to send the second file to the different fixed directory,
than the first one ?
Code:
#!/usr/bin/perl -w

use strict;
use warnings;
use Net::FTP;

# change directory
chdir "/ftp/files" or die "/ftp/files: $!\n";

# DO NOT transfer without info file
-f "/ftp/files/info" or die "info file is missing\n";

open(FILE, "<info>");
while (<FILE> ) {
s/\W*$//;
next if (!$_);
/^(.+?) \s+ (.+?)$/x;

my ($old, $new) = ($1, $2);
rename $old, $new;

# ftp transfer

my $server = "X.X.X.X";
my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3);
$ftp or die "$server: cannot connect: $@";
# If you don't use ~/.netrc
$ftp->login ('anonymous', '****@domain.net') or
die "$_: cannot logon: " . $ftp->message;

# change remote directories
my $cwd_performed = 0;
if ($cwd_performed) {
$ftp->cwd("FTP/IN/FIRST")
}
else {
$ftp->cwd("FTP/IN/SECOND") if (!$cwd_performed++)
}

# Put first file to the ftp server
$ftp->put ($2) or
die "$server: cannot put $2: " . $ftp->message;
$ftp->quit;

sleep (15 * 60)
}
Code:
Quote Reply
Re: [ccc] need HELP In reply to
 Hi ccc,
The decision on which directory to use is controlled by $cwd_performed which you reset every time just before using it so the result will be the same each time. Try moving "my $cdw_performed = 0;" to the top of the program.

Not sure if you intended to alternate directories and use one once and switch permanently. See my suggested line to alternate if that is what you wanted.

Also, if I read your code correctly, use the $2 return variable from your regular expression well down in your code from where it is set - and after saving the value in your own variable ($new). It's risky to assume these return variables will be stable through many lines of code. You have control over your variables so it would be safer to use them.

See suggested changes in bold:

#!/usr/bin/perl -w

use strict;
use warnings;
use Net::FTP;

my $cwd_performed = 0;

# change directory
chdir "/ftp/files" or die "/ftp/files: $!\n";

# DO NOT transfer without info file
-f "/ftp/files/info" or die "info file is missing\n";

open(FILE, "<info>");
while (<FILE> ) {
s/\W*$//;
next if (!$_);

/^(.+?) \s+ (.+?)$/x;
my ($old, $new) = ($1, $2);

rename $old, $new;

# ftp transfer

my $server = "X.X.X.X";
my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3);
$ftp or die "$server: cannot connect: $@";
# If you don't use ~/.netrc
$ftp->login ('anonymous', '****@domain.net') or
die "$_: cannot logon: " . $ftp->message;

# change remote directories

if ($cwd_performed) {
$cwd_performed = 0; #to switch back to other directory again.
$ftp->cwd("FTP/IN/FIRST")
}
else {
$ftp->cwd("FTP/IN/SECOND") if (!$cwd_performed++)
}

# Put first file to the ftp server
$ftp->put ($new) or
die "$server: cannot put $new: " . $ftp->message;
$ftp->quit;

sleep (15 * 60)
}


I hope this helps. Smile