1832227 Members
2554 Online
110041 Solutions
New Discussion

Perl Net::FTP

 
SOLVED
Go to solution
jerry1
Super Advisor

Perl Net::FTP

Does anyone know how to get around this
wildcard filename problem with perl.

The file I am picking up is in this format
with ";" in it.

TESTDFILE.TXT;11

When I try to do a "get" or "retr" it only
sees the string "1". I would be able to get
the file if I could figure out how to use
"command" with "mget".

$ftp->command (prompt) ;
$ftp->command (mget $file) ;

The result is this:

Net::FTP=GLOB(0x40012894)>>> NLST TESTDFILE*
Net::FTP=GLOB(0x40012894)<<< 150 Opening data connection for TESTDFILE*.
Net::FTP=GLOB(0x40012894)<<< 226 Closing data connection.
Net::FTP=GLOB(0x40012894)>>> prompt
Net::FTP=GLOB(0x40012894)>>> mget TESTDFILE*
Net::FTP=GLOB(0x40012894)>>> QUIT
Net::FTP=GLOB(0x40012894)<<< 500 Invalid command.
TESTDFILE.TXT;11
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Perl Net::FTP

You are making this more difficult by using the Net::Cmd:command method as opposed to simply using the ftp->get() method. I would do an ls to load the filenames into an array and then get() them one at a time. The nice thing about this approach is that get allows an optional LOCAL_FILENAME argument so that you can rename these ugly things "on the fly". The next nice thing is that you can check the status of each and every get.

The semicolon is probably causing you grief because you are not quoting your values.
If it ain't broke, I can fix that.
jerry1
Super Advisor

Re: Perl Net::FTP

I tried quotes.

$ftp->get ("$file");


If you know how to use "mget" with $FTP->
that would be great since "*" is valid with
mget and not get.

A. Clay Stephenson
Acclaimed Contributor

Re: Perl Net::FTP

Forget mget. Do it the smart way leveraging your earlier use of ls:

# First create a function that will fix those dumb pathnames. This one will lowercase them as well but you should be able to figure out how to (un)fix that if you don't want it.



use Net::FTP;
use English;
use constant OKEY_DOKEY => 2;

sub fixname
{
my $fname = $_[0];
if ($fname =~ /(.+);(\d+)/)
{
$fname = lc($1);
}
return($fname);
} # fixname

...
...
# Wildcards will work so it's a good idea
# to load an array


my @tmp_array = $ftp->ls("*.txt");
my $stat = $ftp->status;
my $cc = 0;
if ($stat == OKEY_DOKEY)
{
my $i = 0;
my $newname = '';
while (($i <= $#tmp_array) && ($cc == 0))
{
$newname = fixname($tmp_array[$i]);
$ftp->get($tmp_array[$i],$newname);
$stat = $ftp->status;
if ($stat != OKEY_DOKEY)
{
$cc = ($stat != 0) : $stat : 242;
printf STDERR ("Get of %s failed (%d)\n",
$tmp_array[$i],$cc);
}


++$i;
}
}
else
{
$cc = ($stat != 0) ? $stat : 243;
printf STDERR
("ls of '%s' failed; (%d)\",$Pattern,
$cc);
}
ftp->quit;
exit($cc);
If it ain't broke, I can fix that.