user2206329
user2206329

Reputation: 2842

How to get list of files based on pattern matching using FTP?

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

Upvotes: 4

Views: 4485

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202262

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.

Upvotes: 3

Related Questions