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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With