Get a list of FTP filenames, no additional data
I'm trying to upload the contents of an FTP folder to a local folder using this example on StackOverflow:
Uploading a list of files from ftp to a local folder using C #?
The code I have at the moment:
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 am getting an unmaintainable path exception and when I check the values โโof my variables path
and trnsfrpth
they seem to include 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 grab just the filename, resource-orders-1437019656813-893.json
without meanness ( rightof()
for example)?
source to share
To get just a list of filenames without additional information, use WebRequestMethods.Ftp.ListDirectory
(FTP command NLST
) instead of WebRequestMethods.Ftp.ListDirectoryDetails
(FTP command LIST
).
source to share
Here is the function I'm using:
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 list of files 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 through! They can be sorted with .Sort () and you can do binary searches as well.
source to share