Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Net::FTP usage

by kafkaf55 (Acolyte)
on Aug 13, 2010 at 13:15 UTC ( [id://854906]=perlquestion: print w/replies, xml ) Need Help??

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

I have put together a small test FTP code to test before moving it into my main program. I have research many sites that when I transfer the code over, it runs, but no results. Tried using the "die" portions to display messages, but I don't understand where the messages are going. I am assuming that my entries are correct. Any proofing of the code would help greatly. The "put" is not working, but may not be the problem. Also, the only error message I receive is "Bad remote filename....."

#!/usr/bin/perl
#
use strict ;
use warnings ;
use Net::FTP ;

my $tempdir = "/ccifas/tempifas/AU" ;
my $ftp ;
my $hostname = "ftpsrv" ;
my $username = "user" ;
my $password = "passwd#" ;
my $home = "download/targetdir" ;
my $lsfiles = "/ccifas/script_devl/lsfiles" ;
my @array ;
my $f ;
my $pwd ;

system ("cd $tempdir;ls sudolog* > $lsfiles") ;
open(SDfiles, "$lsfiles" ) or
die "Cannot open the file $!\n" ;
@array = <SDfiles> ;
close SDfiles ;

$ftp = Net::FTP->new($hostname)
or die "Cannot connect to host", $ftp->message ;
$ftp->login($username, $password)
or die "Cannot login to host", $ftp->message ;
$ftp->cwd($home)
or die "Cannot change working directory ", $ftp->message ;
$ftp->dir($home)
or die "Cannot produce the directory listing ", $ftp->message ;
foreach $f(@array)
{
$ftp->put($f) ;
or die "Cannot issue put command to host ", $ftp->message ;
}
$ftp->quit ;

exit ;

Replies are listed 'Best First'.
Re: Net::FTP usage
by Corion (Patriarch) on Aug 13, 2010 at 13:25 UTC

    You haven't looked at what your target filenames actually are, even though the error message tells you that they are bad. Do:

    for my $target (@array) { print "Trying to store [$target]\n"; };

    Then you will see that each target filename still has a newline appended. See chomp on how to remove them, or consider just stripping all trailing whitespace:

    for my $target (@array) { $target =~ s/\s+$//; print "Storing [$target]\n"; $ftp->put( $target ); };
      It is showing me the filename that I expect, just like when I was debugging. Like the first file is sudolog_2010Jul_Infodevl. Now is says "Cannot open Local file sudolog_2010Jul_Infodevl: A file or directory in the path does not exist pointing to the $ftp->put statement. Earlier in the code I cd to the local directory. Should that still exist for this FTP? Ken

        You never call chdir, so you never change your working directory for your Perl script.

        Maybe you want to read about glob instead of shelling out to write the entries of a directory into a file?

        Also, I still don't believe you that you do not have whitespace at the end of the elements of @array.

        use File::Glob 'bsd_glob'; my @files = bsd_glob "$tempdir/sudolog*"; print "Found file [$_]\n" for @files;

        ... will read all the files named sudolog* from the directory given in the $tempdir variable.

        Based on your die statements, I don't think that the issue is with the $ftp->put line. The error message that you mentioned ("Cannot open Local file sudolog_2010Jul_Infodevl: A file or directory in the path does not exist") looks like the die statement from where you tried opening the file.

        I agree with Corion about the problem being that you're in the wrong directory. Unless your Perl script is run in the same directory as the 'lsfiles' file that you're trying to create, the open command will look in the path that the Perl script was invoked from and will fail to find it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-28 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found