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

Anyone know how to retrieve a file that has spaces in the filename using Net::FTP? I've been using Net::FTP for a few months now with no problems but when I tried to use it with a filename containing spaces (e.g. 'Rev 1792.2.zip') it barfed. I've tried putting the filename in quotes which works when I use ftp myself but Net::FTP didn't like it (same error). Here's the code snippet I'm using to get the files. The $ftp->get() works fine unless there's a space in the remote filename then it says:
Bad remote filename 'Rev 1792.2.zip' at ./foo.pl line 400
$ftp->get($fname,$tempfname) || die("couldn't get remote file $fname") +;
Thanks, --RT

Replies are listed 'Best First'.
Re: Spaces in file names with Net::FTP
by AgentM (Curate) on Dec 12, 2000 at 03:40 UTC
    I don't see any indication that this is Windoze thang, so here's the UNIX solution: backslash (\). Insert the backslash in front of the space (and any other characters) so that the FTP server recognizes the file, otherwise it looks like you are sending an invalid GET with bad arguments.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Spaces in file names with Net::FTP
by BigJoe (Curate) on Dec 12, 2000 at 02:13 UTC
    if the file is on a windows machine you can use it's dos name which you can get by doing a dir/x/b the /x gives you non extended name and /b is bare so you don't get the exact path. It usually would be REV1792~1.zip.

    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.
    Use the source Luke.
Re: Spaces in file names with Net::FTP
by the_slycer (Chaplain) on Dec 12, 2000 at 03:30 UTC
    Well, to get a file with spaces using regular ftp, you must enclose the filename in quotes. eg filename is blah blah, 'get blah blah' tries to get blah. Whereas 'get "blah blah" works fine. So, you have have to enclose that $fname in quotes somehow.
(RhetTbull) Re: Spaces in file names with Net::FTP
by RhetTbull (Curate) on Dec 12, 2000 at 21:50 UTC
    Thanks everyone for the tips. After trying to put the filename in both single and double quotes and escaping the spaces with \, I still kept getting the same error -- even though these solutions worked when I manually logged into the FTP server...so, I turned to the source. After finding the source for the FTP.pm that was installed on my system I found this:
    sub get { ... croak("Bad remote filename '$remote'\n") if $remote =~ /[\s\r\n]/s;
    So, it looks like FTP->get was throwing out the filename before it ever requested it from the server. I checked on CPAN and sure enough, I had a really old libnet and Net::FTP installed. Newer versions do allow spaces in the filename.
    perl -MCPAN -e 'install Bundle::libnet'
    cured all my woes. Thanks again for the suggestions. --RT