Handling FTP Directories with Spaces


Some FTP servers do not correctly handle paths that contain spaces when they are passed as an argument to a directory listing command. For example, listing a directory such as /Shared/Monthly Reports by setting RemoteFile to its path may fail, or may list the wrong directory.

This happens because the arguments of the FTP listing commands (LIST and NLST) are not standardized, and many servers pass them to a command such as ls, which treats each space-separated word as a separate argument. Other commands, such as CWD (change directory), RETR (download), and STOR (upload), treat everything after the command name as the path, so they handle spaces correctly.

Listing a Directory with Spaces in Its Path

To list a directory whose path contains spaces, change into the directory first using the ChangeRemotePath method, and then list the current directory by calling ListDirectory or ListDirectoryLong with RemoteFile set to an empty string. Since no path is sent with the listing command, the server does not need to interpret the spaces.

FTP ftp = new FTP(); ftp.RemoteHost = "ftp.example.com"; ftp.User = "USER"; ftp.Password = "PASSWORD"; ftp.Logon(); // Change into the directory, and then list the current directory. ftp.ChangeRemotePath("/Shared/Monthly Reports"); ftp.RemoteFile = ""; ftp.ListDirectoryLong(); foreach (DirEntry entry in ftp.DirList) { Console.WriteLine(entry.FileName + (entry.IsDir ? " (directory)" : "")); } ftp.Logoff();

The QueryRemotePath method can be used to confirm the current directory on the server.

Filtering the Listing

To list only certain files (e.g., files matching *.pdf), RemoteFile is normally set to a file mask, which is sent to the server with the listing command. Since file masks are not standardized either, some servers reject them, or have trouble with masks that contain spaces. In this case, enable the ApplyFileMaskLocally configuration setting, which causes the component to list the whole directory and apply the file mask itself.

ftp.ChangeRemotePath("/Shared/Monthly Reports"); // Apply the file mask locally, rather than sending it to the server. ftp.Config("ApplyFileMaskLocally=true"); ftp.RemoteFile = "*.pdf"; ftp.ListDirectoryLong();

Downloading and Uploading Files with Spaces

Paths with spaces can be used directly when downloading, uploading, and deleting files, since the corresponding FTP commands treat the rest of the line as the path. Set RemoteFile to the file's path (either an absolute path, or a path relative to the current directory), and call the appropriate method.

// Download a file whose name contains spaces. ftp.RemoteFile = "/Shared/Monthly Reports/March Summary.pdf"; ftp.LocalFile = "C:\\reports\\March Summary.pdf"; ftp.Download();

The same approach works for FTPS (FTP over TLS), which is supported by the FTP component through the SSLStartMode property.

Note that in older versions of IPWorks, the current directory was changed by sending the CWD command with the Command property, which has been replaced by the SendCommand method. In current versions, use the ChangeRemotePath method instead, which sends the CWD command automatically.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.