Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Net::FTP Server does not support cwd...alternatives?

by linebacker (Scribe)
on Jun 14, 2002 at 17:00 UTC ( #174633=perlquestion: print w/replies, xml ) Need Help??

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

I need to autmate an ftp session where if the particular file I am looking for on the ftp server is newer than the file I have on my local machine I want to download it. Before I can get to the part where I compare dates (I'm looking for suggestions please), I can't even get a decent session to run because the server won't do cwd. I do not have any control over the server side at all.
I used Net::FTP, but the ftp server does not support the cwd command Net::FTP needs to work. Any suggestions?
*NOTE Net::FTP does not support the cd command either.
#!/usr/bin/perl -w # ----------------------------------------------------------- # FTP script # ----------------------------------------------------------- use Net::FTP; $host = ''; $user = 'anonymous'; $pass = ''; $remote_dir = '/pub/path/to/files'; $download_filter = "*.myfile.exe"; $destination_dir = "./"; print "Opening FTP connection to $host\n"; my $ftpBox = Net::FTP->new($host) || die "failed to connect to $host $!"; $ftpBox->login($user, $pass) || die "failed to log onto $host $!"; $ftpBox->pasv(); if($remote_dir !~ /^ *$/) { print "Changing remote directory to $remote_dir\n"; $ftpBox->cd($remote_dir) || die "failed to cwd to $remote_dir on $host ftp $!"; } @file_list = $ftpBox->ls($download_filter); foreach $filename (@file_list) { print "getting $filename\n"; $ftpBox->get($filename, $destination_dir . $filename) || die "failed to get $filename to $destination_file"; } print "Closing FTP download connection...\n"; $ftpBox->quit;

Replies are listed 'Best First'.
Re: Net::FTP Server does not support cwd...alternatives?
by kvale (Monsignor) on Jun 14, 2002 at 17:34 UTC
    It is not necessary to cd to directories to query their contents. Simply use the relative or absolute pathname to the directories you want to use:
    use Net::FTP; $ftp = Net::FTP->new("", Debug => 0); $ftp->login("anonymous",'-anonymous@'); $ftp->dir("/usr/local/ftp/mark"); $ftp->get("/usr/local/ftp/mark/that.file"); $ftp->quit;
      Thanks for the responses. As soon as I read them I thought why didn't I think of that. Tunnel Vision. Thanks a lot!
Re: Net::FTP Server does not support cwd...alternatives?
by atopolc (Pilgrim) on Jun 14, 2002 at 17:26 UTC
    You can use the path in your call to get. Replace your call to get with this one.
    $ftpBox->get("$remotedir/$filename","$destination_dir/$filename") or die "failed to get $filename to $destination_file";

    If your just interested in checking to see if a directory exists. Then an ls or dir is almost as good as cd.

    $ftpBox->ls($remotedir); #or $ftpBox->dir($remotedir);
    Updated: Added a little more info.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://174633]
Approved by shotgunefx
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2019-10-18 23:54 GMT
Find Nodes?
    Voting Booth?