9

I'm trying to download the contents of an FTP folder to a local folder using a this example on StackOverflow:
Downloading a list of files from ftp to local folder using c#?

The code I have at the moment is:

public void DownloadFilesFromFTP(string localFilesPath, string remoteFTPPath)
{
    remoteFTPPath = "ftp://" + Hostname + remoteFTPPath;
    var request = (FtpWebRequest)WebRequest.Create(remoteFTPPath);

    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    request.Credentials = new NetworkCredential(Username, Password);
    request.Proxy = null;

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    List<string> directories = new List<string>();

    string line = reader.ReadLine();

    while (!string.IsNullOrEmpty(line))
    {
        directories.Add(line);
        line = reader.ReadLine();
    }
    reader.Close();

    using (WebClient ftpClient = new WebClient())
    {
        ftpClient.Credentials = new System.Net.NetworkCredential(Username, Password);

        for (int i = 0; i <= directories.Count - 1; i++)
        {
            if (directories[i].Contains("."))
            {

                string path = remoteFTPPath + @"/" + directories[i].ToString();
                string trnsfrpth = localFilesPath + @"\" + directories[i].ToString();

                ftpClient.DownloadFile(path, trnsfrpth);
            }
        }
    }

    response.Close();
}

I'm receiving a path not supported exception and when I inspect the values of my variables path and trnsfrpth they appear to be including Apache information.

path: ftp://hostname/data/resourceOrders/-rw-r--r-- 1 apache
apache 367 Jul 16 14:07 resource-orders-1437019656813-893.json

And

trnsfrpth: V:\code.runner\local\orders-rw-r--r-- 1 apache apache
367 Jul 16 14:07 resource-orders-1437019656813-893.json

How can I capture just the filename, resource-orders-1437019656813-893.json without a hacky (rightof(), for example) approach?

Community
  • 1
  • 1
Michael A
  • 9,480
  • 22
  • 70
  • 114

2 Answers2

13

To retrieve just list of file names without additional details, use WebRequestMethods.Ftp.ListDirectory (FTP command NLST), instead of WebRequestMethods.Ftp.ListDirectoryDetails (FTP command LIST).

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
3

Here is the function that I use:

public class FileName : IComparable<FileName>
{
    public string fName { get; set; }
    public int CompareTo(FileName other)
    {
        return fName.CompareTo(other.fName);
    }
}

public static void getFileList(string sourceURI, string sourceUser, string sourcePass, List<FileName> sourceFileList)
{
    string line = "";
    FtpWebRequest sourceRequest;
    sourceRequest = (FtpWebRequest)WebRequest.Create(sourceURI);
    sourceRequest.Credentials = new NetworkCredential(sourceUser, sourcePass);
    sourceRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    sourceRequest.UseBinary = true;
    sourceRequest.KeepAlive = false;
    sourceRequest.Timeout = -1;
    sourceRequest.UsePassive = true;
    FtpWebResponse sourceRespone = (FtpWebResponse)sourceRequest.GetResponse();
    //Creates a list(fileList) of the file names
    using (Stream responseStream = sourceRespone.GetResponseStream())
    {
        using (StreamReader reader = new StreamReader(responseStream))
        {
            line = reader.ReadLine();
            while (line != null)
            {
                var fileName = new FileName
                        {
                            fName = line
                        };
                        sourceFileList.Add(fileName);
                line = reader.ReadLine();
            }
        }
    }   
}

Set your sourceURI, user and password in main() and declare a filelist like:

List<FileName> sourceFileList = new List<FileName>();
string sourceURI = "ftp://www.sourceftp.com/";
string sourceUser = "testUser";
string sourcePass = "testPass";

This will give you an easily iterable list of filenames(sourceFileList[i].fName) to go thru! These can be sorted using .Sort() and you can also do Binary searches.

John Boling
  • 464
  • 6
  • 14