1

I'm using Net::FTP::Foreign and I keep getting an error about file not being found and I'm not sure if my syntax is wrong or I'm misunderstanding how to use it.

$LOGFILE = 'data_log' . $YYYYMMDD . '.log';

$sftp->setcwd('/tmp') 
    or die 'Unable to change working directory to /tmp: ' . $sftp->error;
print "CWD set\n";
my $ls = $sftp->ls('/tmp', names_only => 1, ordered => 1);

foreach my $file (@$ls) {
    print $file . "\n";
}

print 'Getting file: ' . $LOGFILE . "\n";
$sftp->get('/tmp/data_log*', 'data_log' . $YYYYMMDD . 'log') 
        or die 'Could not get remote file: ' . $sftp->error;

The error I get is that no such files exist on the remote side but I've confirmed that they do exist when I do the LS cmd.

Is there anything blatantly wrong with my script which is causing it to not work?

Also I'm using Net::SFTP::Foreign because Net::SFTP won't build on my MBP running 10.7

2 Answers 2

2

Instead of get use mget that will transfer all the files matching the given pattern.

1

You have:

$sftp->get('/tmp/data_log*', 'data_log' . $YYYYMMDD . 'log') 

Is there a file named /tmp/data_log*? Or do you want to get all the files in /tmp that start with data_log?

You can only fetch one file at a time with the get method. Globs don't work. Why not move your get into your foreach loop? When you see a file you want, nab it.

foreach my $file ( @{$ls} ) {
    print qq(Found file "$file" in directory\n);

    # Is this the file we want to fetch?

    if ( $file eq $LOGFILE ) {
          print qq(Attempting to fetch "$file"\n);
          $sftp->get( "$LOGFILE" )   #I think you're only interested in this file
              or die qq(Could not fetch file "$LOGFILE" from directory: ) . $sftp->error;
          print qq(Fetched file "$file"\n);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.