user2206329 user2206329 -4 years ago 51
C# Question

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

(which is like 10 files).
But new files get added every 10 minutes.

So I only need to get the files that start with
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...



So I have managed to get the correct listing... but to download the files do I have to something like the following?

int bytesRead = 0;
byte[] buffer = new byte[2048];

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpSourceFilePath);
request.Credentials = new NetworkCredential("anonymous", "");

request.Method = WebRequestMethods.Ftp.DownloadFile;

Stream reader = request.GetResponse().GetResponseStream();
FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);

while (true)
bytesRead = reader.Read(buffer, 0, buffer.Length);

if (bytesRead == 0)

fileStream.Write(buffer, 0, bytesRead);

I thought it would be a simpler thing like after looping through the files.... I'm new to all this ftp stuff...

Answer Source

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 =
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);

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download