4

I need to get a list of files from an FTP server.

The FTP server has over 10, 000 files.

I only need the files that start with ABC... (which is like 10 files). But new files get added every 10 minutes.

So I only need to get the files that start with ABC that have been created in the last 10 minutes.

How do I achieve this? Can I do this from C# natively?

What I have seen so far, I can connect to the FTP server, get a list of ALL the files and check the name of each one... this seems like it would take a long time if the number of files increases...

Ta

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user2206329
  • 2,792
  • 10
  • 54
  • 81
  • if i was you i would have a little console application sitting somewhere which keeps calling the FTP server every 10 minutes and updates a JSON file or even a database with the file names. This means you can query the JSON file or database without worrying about speed via your application. This is the approach i have done for many problems like the above. Yes everything you said above is very possible with c# and would not be complex to do.. give me a shout if you need a hand or have any code to show! – Josh Stevens Jun 11 '17 at 15:01
  • Josh - Thank you so much... I appreciate your help – user2206329 Jun 12 '17 at 08:41

1 Answers1

3

In general, there's no other way than the one you know: retrieve list of all files and filter them locally.


But many servers support non-standard/proprietary filtering of the listing.

If you are lucky and your FTP server do support this, you can use a file mask to retrieve only a subset of files. In your case the mask would by usually like ABC*. Most major FTP servers do support * pattern.

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/ABC*");
request.Credentials = new NetworkCredential("username", "password");  
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());

For a partial list of supported patterns of common FTP servers, see my answer to FTP directory partial listing with wildcards.

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