in reply to Re: Re: Problem using Net::FTP
in thread Problem using Net::FTP
the info file looks :However, because the code looks like:
----------------------
AB00001 N120001
BB00002 N200023
----------------------
you will process as many files as the info file has lines. So, if the info file were to be replaced with something like this:while (<FILE> ) {
then your code will attempt to ftp and rename each of those 4 files.AB00001 N120001 BB00002 N200023 CB00003 N300011 DB00004 N400022
If you want to limit the number of files processed to always be two and only two then you don't want to use a while loop. Perhaps a for loop would be better:
# replace the while(<FILE>) { with the following two lines for(my $i = 1; $i <= 2; $i++) { $_ = <FILE>;
script should check if the files are complete and names on info file matches to the names of this 2 files.I have no idea what this means.
first file N120001 should be transfered to: $ftp->cwd("FTP/A1"); and second file N200023 to: $ftp->cwd("FTP/B2");If you can't change what gets put into the info file then my suggestion the other day won't work as well. If you can change it then that would probably make better sense. This way the directory names to ftp things to aren't hard coded in your script and therefore may be easier to change in future.
Assuming you can't make the changes to the info script then you might want to change your script to look more like:
Note that this still allows you to deal with more than 2 files in the info file, but only if you add additional ftp locations into the array up the top.# ftp directories my @ftp_locations = ("FTP/A1", "FTP/B2"); # open the file safely or complain it wouldn't open open(FILE, "<", "info") or die "Failed to open info file: $!"; while (<FILE> ) { s/\W*$//; # remove trailing whitespace next if (!$_); # skip empty lines # check that we get our match. If not, # complain and move on. Want to see two # filenames. unless(/^([\w.-]) \s+ ([\w.-])$/x) { print STDERR "$_ is not a valid line"; next; } my ($old, $new) = ($1, $2); rename $old, $new; # Now that we have our filenames, grab an # ftp directory from the list unless(@ftp_locations) { die "Not enough specified ftp_locations for ". "given number of files!"; } my $destination = shift @ftp_locations; # 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', 'mail@address') or die "$_: cannot logon: " . $ftp->message; # change remote directory for the first file $ftp->cwd($destination); # Send file to ftp server $ftp->put($new) or die "$server: cannot put $new: " . $ftp->message; # Sleep for 20 minutes before processing next file. sleep (20 * 60) }
when the transfer completed should create a new subdirectory with date and time, something like: 200402241200 and put (backup) all files there, otherwise files will be overwritten each time.How about you give this bit a go and get back to us with how you do at it?
and I would like use your script, that you already posted.Ah, but of course. Certainly, go ahead and use the code I've provided you with. Good luck with the rest.
Hope this helps,
jarich
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Problem using Net::FTP
by cc (Beadle) on Mar 10, 2004 at 01:49 UTC | |
by jarich (Curate) on Mar 12, 2004 at 03:24 UTC | |
by cc (Beadle) on Mar 14, 2004 at 02:11 UTC | |
by jarich (Curate) on Mar 16, 2004 at 01:29 UTC | |
by cc (Beadle) on Mar 25, 2004 at 11:45 UTC | |
|