Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Net::FTP->dir("abc def"); embedded spaces in filename fails.

by justanyone (Acolyte)
on Jul 17, 2002 at 14:55 UTC ( #182444=categorized question: print w/replies, xml ) Need Help??
Contributed by justanyone on Jul 17, 2002 at 14:55 UTC
Q&A  > HTTP and FTP clients



I'm trying to retrieve files and directories off an FTP server where the filenames and directory names both have embedded spaces. Microsoft allows and encourages spaces in filenames, so this is probably common.

I'm trying to use Net::FTP to get a directory listing of a dir with embedded spaces in the name, and also do an FTP get on the files in that directory. Alas, this seems impossible.

The FTP server is "Microsoft FTP Service (Version 5.0)", the client is on Solaris, but I don't think it matters. I'm using Net::FTP 2.64.

I've tried and failed with:

  • Net::FTP->dir("\"abc def\"");
  • Net::FTP->dir("\'abc def\'");
  • Net::FTP->dir("abc\ def");
  • Net::FTP->dir("abc%20def");

Answer: Net::FTP->dir(
contributed by amphiplex

Try using the ls() method, it worked for me connecting to a wuftpd on linux.

Looks as if dir() generates a LIST command, while ls() generates an NLST command. The former calls /bin/ls.

debug output:

Net::FTP=GLOB(0x8293290)<<< 220 FTP server (Version 6. +5/OpenBSD, linux port 0.3.2) ready. Net::FTP=GLOB(0x8293290)>>> user kurt Net::FTP=GLOB(0x8293290)<<< 331 Password required for kurt. Net::FTP=GLOB(0x8293290)>>> PASS .... Net::FTP=GLOB(0x8293290)<<< 230- Have a lot of fun... Net::FTP=GLOB(0x8293290)<<< 230 User kurt logged in. Net::FTP=GLOB(0x8293290)>>> CWD /home/kurt/ Net::FTP=GLOB(0x8293290)<<< 250 CWD command successful. Net::FTP=GLOB(0x8293290)>>> PASV Net::FTP=GLOB(0x8293290)<<< 227 Entering Passive Mode (127,0,0,1,183,2 +25) Net::FTP=GLOB(0x8293290)>>> LIST test file Net::FTP=GLOB(0x8293290)<<< 150 Opening BINARY mode data connection fo +r '/bin/ls'. Net::FTP=GLOB(0x8293290)<<< 226 Transfer complete. DIR: [in.ftpd: file: No such file or directoryin.ftpd: test: No such f +ile or directory] Net::FTP=GLOB(0x8293290)>>> PASV Net::FTP=GLOB(0x8293290)<<< 227 Entering Passive Mode (127,0,0,1,183,2 +27) Net::FTP=GLOB(0x8293290)>>> NLST test file Net::FTP=GLOB(0x8293290)<<< 150 Opening BINARY mode data connection fo +r 'file list'. Net::FTP=GLOB(0x8293290)<<< 226 Transfer complete. LS: [test file]
generated by
$ftp = Net::FTP->new($destination, Debug=>2) || die "Couldn't connect: + $!\n"; $ftp->login($username,$password) || die "Couldn't log in: $!\n"; $ftp->cwd($path) || die "couldn't cwd! $!\n"; print STDERR "DIR: [",$ftp->dir($filename),"]\n"; print STDERR "LS: [",$ftp->ls($filename),"]\n";
--- amphiplex
Answer: Net::FTP->dir(
contributed by fedelman

Hi! It's simple.

You need run the next commands:
$filename =~ s/\s/\\ /g;

This is the correct way to escape the whitespaces.
That's all!

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [marioroy]: choroba++, Discipulus++. It depends on the type of module. Data-type "only" modules are likely multi-process safe, re: Hash::Ordered, Tie::IxHash.
    [marioroy]: ... when shared via MCE::Share-> share(...)
    [marioroy]: Net type modules are likely not multi-process safe unless stated in the documentation.
    [marioroy]: The Prima author fixed his module to be both thread and multi-process safe. Thanks Dmitry.
    [marioroy]: Of all the GUI-type modules, Prima was the worst regarding thread/multi- process safety. Now, it's the best for safety. ;-)
    [marioroy]: Tk, Gtk2, Gtk3 requires extra care.

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (8)
    As of 2017-09-22 10:13 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (260 votes). Check out past polls.