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

hi

I need HELP to change this perl script.
Jarich is an excellent programmer and she helped me a lot before.
this script is looking for and opens this INFO text file:
...................................
FILE1 NN20000610
FILE2 ZZ20000610
...................................
renames the first file from FILE1 to NN20000610, sends it via ftp to the remote_directory_1,
waits 20 minutes,
renames the second filr from FILE2 to ZZ20000610 ans sends via ftp to the remote_directory_2

it works well,but my really problem is I cannot send a second file, if the first file was not processed.
that means, if the first file will be processed correctly,it should disappear on the remote server within 10-15 minutes.

I need to change this script, after the first file was sent:
- wait 20 minutes
- try to download via ftp the first file from the remote server (from remote_directory_1)
- if the first file is still there (not processed) don't send the second file and close the program
- but if the first file is not there (it was processed correctly), send the second file immediately.
#!/usr/bin/perl -w use strict; use warnings; use File::Copy; use Net::FTP; use Net::Netrc; my $server = "192.168.0.10"; my $user = "myuser"; my $password = "mypassword"; # ftp directories my $remote_directory_1 = "/FTP/DATA1"; my $remote_directory_2 = "/FTP/DATA2"; chdir "/ftp/IN" or die "/ftp/IN: $!\n"; -f "/ftp/IN/INFO" or die "NO INFO FILE NO TRANSFER !\n"; my @ftp_locations = ($remote_directory_1, $remote_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 file 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!"; } # 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; # Sleep for 20 minutes before processing next file. sleep (20 * 60) }
if someone has time and can help me to solve this problem I'll be very happy.
greetings !

20040610 Edit by Corion: Changed title from 'need help to change this perl script'

Replies are listed 'Best First'.
Re: need help with FTP script
by Roy Johnson (Monsignor) on Jun 10, 2004 at 15:16 UTC
    After your sleep, you just want to check whether $destination/$new is still there, right? So you repeat the connection and navigate to the directory, do an ls, and look for the file. Something like (absolutely untested):
    # This is just like the section before the sleep, but it's aft +er $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); # # this is new my $found = grep /\Q$new\E/, $ftp->ls; if ($found) { die "The file is still there!\n" }

    The PerlMonk tr/// Advocate
      hi Roy

      thanks for your time and help
      about your question:
      I have to send the first file in each cases.
      after 20 minutes sleep I have to check if the first file is still on the remote server in $remote_directory_1 or not.
      only if the first file is not there (in $remote_directory_1), I can send the second file.

      I changed the code :
      ...................................................................... +. # 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; # Sleep for 20 minutes before processing next file. sleep (20 * 60) $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); # + (this is 172 line) $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); # # this is new my $found = grep /\Q$new\E/, $ftp->ls; if ($found) { die "The file is still there!\n" } ...................................................................... +... # ftp transfer
      but I get following error:
      "Scalar found where operator expected at send.cgi line 172, near ")
      $ftp"
      (Missing operator before $ftp?)
      syntax error at ftp5.cgi line 172, near ")
      $ftp "
      Execution of ftp5.cgi aborted due to compilation errors."

      the 172 line is:
      $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); # (this is 172 line)

      any idea what could be wrong ?

      kind regards
      cc
        You're missing a semicolon on the previous line.

        We're not really tightening our belts, it just feels that way because we're getting fatter.
Re: need help with FTP script
by shonorio (Hermit) on Jun 10, 2004 at 14:01 UTC
    Hi cc it's look like a homework and because this will be hard someone help you on rewritting you code.

    I did't ckeck all the code, but like it's working ok, maybe you problem is if the file1 still on remote directory. To check if this file still there you can use '@Files = ftp->ls ($destination)' to get files list, and look at @Files to check it.

    You are looking for a good example of NET::FTP try ftp_mirror.pl

    Solli Moreira Honorio
    Sao Paulo - Brazil