Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: Re: Re: Re: Re: Re: Problem using Net::FTP

by jarich (Curate)
on Mar 16, 2004 at 01:29 UTC ( [id://336902]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Re: Re: Problem using Net::FTP
in thread Problem using Net::FTP

The problem is that you haven't actually read what I've suggested. In the pseudocode, that you've written (cut'n'pasted), you parrot back to me the idea of using two separate loops and of pushing the filename onto an array but you've kept your single loop structure.

Of course it's not going to work.

The specific problem of what is wrong is (because you're doing all of this in a single loop) when you chdir into your backup directory. Because you never chdir out again, you never find the second file and so your program halts there.

You should be getting an error about that, but you haven't included that in your message. :(

Anyway, the code that follows is probably what you're looking for. Compare it with the pseudocode I suggested to you. See how we have 2 loops and a check between them and all the things I suggested.

Notice too, that I've changed your backup directory name. If you don't like my suggestion then feel free to change it back again.

#!/usr/bin/perl -wT use strict; use File::Copy; ... chdir "/var/files" or die "/var/files: $!\n"; -f "/var/files/info" or die "No info NO TRANSFER !\n"; my @ftp_locations = ($directory_1, $directory_2); # open the file safely or complain it wouldn't open open(FILE, "<", "info") or die "Failed to open info file: $!"; # Read all the lines in from info my @files; for(my $i = 1; $i <= @ftp_locations; $i++) { $_ = <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; } # rename the files as per rename in file my ($old, $new) = ($1, $2); unless( -e $old ) { print STDERR "$old does not exist!\n"; next; } rename $old, $new; push @files, $new; } # check that we have the number of files that we expect unless(@files == @ftp_locations) { die "Not enough specified ftp_locations for ". "given number of files!"; } # create backup subfolder my @dt = localtime; my $subfoldername = ($dt[5]+1900) . "-" . ($dt[4]+1) . "-$dt[3]:$dt[2]:$dt[1]_$$"; mkdir "/var/files/out/$subfolder_name" or die "$subfolder_name: $!"; # FTP each file across, die on errors foreach my $new (@files) { my $destination = shift @ftp_locations; # ftp transfer my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3) +; $ftp or die "$server: cannot connect: $@"; # If you don't use ~/.netrc $ftp->login ($user,$password) 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; #Quit FTP When finished $ftp->quit; # move file to backup directory unless(move("$new", "/var/files/out/$subfolder_name")) { print STDERR "Oops! Couldn't move the file: $!"; } # Sleep for 20 minutes before processing next file. sleep (20 * 60) } # If we get to this point then we've renamed, ftped and moved the file +s # so send an email saying so...
Good luck with this.

jarich

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Re: Re: Problem using Net::FTP
by cc (Beadle) on Mar 25, 2004 at 11:45 UTC
    hi jarich

    thank you very much for your time and HELP
    now it works perfectly !
    and very sorry, I didn't answer so quickly, because I was in vacation

    I have only a small problem:
    I try to log the whole process and put:
    # log the whole process BEGIN { use CGI::Carp qw(carpout); my $errorlog = "/var/logs/log.txt"; open(LOG, ">$errorlog") or die("Unable to open $errorlog: $!\n"); print LOG "Errors:\n"; carpout(*LOG); }
    on the top of the script.

    It works and I get the log.
    On the end I try to move all files: 2 data files, info file and log.txt to the backup directory:
    ............................................................ # move the file to the backup subfolder unless(move("$new", "/var/files/out/$subfolder_name")) { print STDERR "Oops! Couldn't move the file: $!"; } system("mv /var/files/info /var/files/out/$subfolder_name"); system("mv /var/logs/log.txt /var/files/out/$subfolder_name"); ............................................................
    it works and all files are moved, but I get in the log following error messages :
    mv: cannot stat `/var/files/info': No such file or directory
    mv: cannot stat `/var/logs/log.txt': No such file or directory

    I don't know what's wrong and why I get these error messages.

    greetings
    cc
      1. Read perldoc File::Copy or if that's too hard read the html version over at CPAN.

      2. Read my previous posts to you, particularly the bits about File::Copy.

      3. Wonder why after the above you're trying to use mv through system when I've shown you a better way.

      4. Realise that you're doing that mv inside a loop so of course the damn files aren't going to be there the next time around the loop!

      5. Change calls to system to use move from File::Copy instead and change these lines to be outside of the second for loop, eg the place where you send the email. Remember to then check that your move succeeded (by using the unless statements like I did).

      6. Rejoice because it all finally works the way it should.

      Hope it helps.

      jarich

        hi jarich

        thanks again !
        with move works well.

        sorry for asking again, but I have other question
        howto check if the first file was transfered succesfully
        and be 100% sure, that the first file is already on the remote server
        and only then send the second file ?
        my really problem is, I cannot send the second file, when the first file is not there.

        greetings
        cc

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-25 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found