如何使用FTP基于模式匹配获取文件列表? [英] How to get list of files based on pattern matching using FTP?
问题描述
我需要从FTP服务器获取文件列表.
I need to get a list of files from an FTP server.
FTP服务器有超过10,000个文件.
The FTP server has over 10, 000 files.
我只需要以ABC...
开头的文件(类似于10个文件).
但是新文件每10分钟添加一次.
I only need the files that start with ABC...
(which is like 10 files).
But new files get added every 10 minutes.
所以我只需要获取最近10分钟内创建的以ABC
开头的文件.
So I only need to get the files that start with ABC
that have been created in the last 10 minutes.
我该如何实现?我可以从C#本地执行此操作吗?
How do I achieve this? Can I do this from C# natively?
到目前为止,我可以连接到FTP服务器,获取所有文件的列表并检查每个文件的名称...如果文件数量增加,这似乎将花费很长时间. ...
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.
如果幸运的话,您的FTP服务器确实支持此功能,则可以使用文件掩码仅检索文件的子集.在您的情况下,遮罩通常类似于ABC*
.大多数主要的FTP服务器都支持*
模式.
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());
有关受支持的普通FTP服务器模式的部分列表,请参阅我对 FTP目录部分列表(带通配符)的回答.
这篇关于如何使用FTP基于模式匹配获取文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!