在 C# 中根据日期时间获取 FTP 文件详细信息
Get FTP file details based on datetime in C#
问题: 我想根据一些特定的日期时间从 FTP 服务器获取文件详细信息,而不使用任何第 3 方。
问题: 我的 FTP 服务器包含 1000 多个文件,因此获取所有文件并在过滤之后需要时间。
有什么更快的方法吗?
string ftpPath = "ftp://directory/";
// Some expression to match against the files...do they have a consistent
// name? This example would find XML files that had 'some_string' in the file
Regex matchExpression = new Regex("^test.+\.xml$", RegexOptions.IgnoreCase);
// DateFilter
DateTime cutOff = DateTime.Now.AddDays(-10);
List<ftplineresult> results = FTPHelper.GetFilesListSortedByDate(ftpPath, matchExpression, cutOff);
public static List<FTPLineResult> GetFilesListSortedByDate(string ftpPath, Regex nameRegex, DateTime cutoff)
{
List<FTPLineResult> output = new List<FTPLineResult>();
FtpWebRequest request = FtpWebRequest.Create(ftpPath) as FtpWebRequest;
ConfigureProxy(request);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = request.GetResponse() as FtpWebResponse;
StreamReader directoryReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
var parser = new FTPLineParser();
while (!directoryReader.EndOfStream)
{
var result = parser.Parse(directoryReader.ReadLine());
if (!result.IsDirectory && result.DateTime > cutoff && nameRegex.IsMatch(result.Name))
{
output.Add(result);
}
}
// need to ensure the files are sorted in ascending date order
output.Sort(
new Comparison<FTPLineResult>(
delegate(FTPLineResult res1, FTPLineResult res2)
{
return res1.DateTime.CompareTo(res2.DateTime);
}
)
);
return output;
}
Problem : My FTP server contains 1000s of files so geting all files and after that filtering it takes time.
Is there any Quicker way to do this ?
没有.
唯一的标准 FTP API 是 LIST
命令及其同伴。所有这些都会为您提供文件夹中 所有文件 的列表。没有 FTP API 可以为您提供按时间戳过滤的文件。
一些 服务器在LIST
命令中支持非标准 文件掩码。
所以他们将只允许您 return *.xml
个文件。
参见
类似问题:
- Download files from FTP if they are created within the last hour
我有一个替代解决方案来使用 FluentFTP 来完成我的功能。
解释:
我正在从 FTP(需要读取权限)下载具有相同文件夹结构的文件。
所以每次 job/service 运行时我都可以检查物理路径是否存在相同的文件(完整路径)如果不存在则可以将其视为新文件。 Ii 也可以做一些相同的操作并下载。
它只是一个替代解决方案。
代码更改:
private static void GetFiles()
{
using (FtpClient conn = new FtpClient())
{
string ftpPath = "ftp://myftp/";
string downloadFileName = @"C:\temp\FTPTest\";
downloadFileName += "\";
conn.Host = ftpPath;
//conn.Credentials = new NetworkCredential("ftptest", "ftptest");
conn.Connect();
//Get all directories
foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
FtpListOption.Modify | FtpListOption.Recursive))
{
// if this is a file
if (item.Type == FtpFileSystemObjectType.File)
{
string localFilePath = downloadFileName + item.FullName;
//Only newly created files will be downloaded.
if (!File.Exists(localFilePath))
{
conn.DownloadFile(localFilePath, item.FullName);
//Do any action here.
Console.WriteLine(item.FullName);
}
}
}
}
}
问题: 我想根据一些特定的日期时间从 FTP 服务器获取文件详细信息,而不使用任何第 3 方。
问题: 我的 FTP 服务器包含 1000 多个文件,因此获取所有文件并在过滤之后需要时间。
有什么更快的方法吗?
string ftpPath = "ftp://directory/";
// Some expression to match against the files...do they have a consistent
// name? This example would find XML files that had 'some_string' in the file
Regex matchExpression = new Regex("^test.+\.xml$", RegexOptions.IgnoreCase);
// DateFilter
DateTime cutOff = DateTime.Now.AddDays(-10);
List<ftplineresult> results = FTPHelper.GetFilesListSortedByDate(ftpPath, matchExpression, cutOff);
public static List<FTPLineResult> GetFilesListSortedByDate(string ftpPath, Regex nameRegex, DateTime cutoff)
{
List<FTPLineResult> output = new List<FTPLineResult>();
FtpWebRequest request = FtpWebRequest.Create(ftpPath) as FtpWebRequest;
ConfigureProxy(request);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = request.GetResponse() as FtpWebResponse;
StreamReader directoryReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
var parser = new FTPLineParser();
while (!directoryReader.EndOfStream)
{
var result = parser.Parse(directoryReader.ReadLine());
if (!result.IsDirectory && result.DateTime > cutoff && nameRegex.IsMatch(result.Name))
{
output.Add(result);
}
}
// need to ensure the files are sorted in ascending date order
output.Sort(
new Comparison<FTPLineResult>(
delegate(FTPLineResult res1, FTPLineResult res2)
{
return res1.DateTime.CompareTo(res2.DateTime);
}
)
);
return output;
}
Problem : My FTP server contains 1000s of files so geting all files and after that filtering it takes time.
Is there any Quicker way to do this ?
没有.
唯一的标准 FTP API 是 LIST
命令及其同伴。所有这些都会为您提供文件夹中 所有文件 的列表。没有 FTP API 可以为您提供按时间戳过滤的文件。
一些 服务器在LIST
命令中支持非标准 文件掩码。
所以他们将只允许您 return *.xml
个文件。
参见
类似问题:
- Download files from FTP if they are created within the last hour
我有一个替代解决方案来使用 FluentFTP 来完成我的功能。
解释:
我正在从 FTP(需要读取权限)下载具有相同文件夹结构的文件。
所以每次 job/service 运行时我都可以检查物理路径是否存在相同的文件(完整路径)如果不存在则可以将其视为新文件。 Ii 也可以做一些相同的操作并下载。
它只是一个替代解决方案。
代码更改:
private static void GetFiles()
{
using (FtpClient conn = new FtpClient())
{
string ftpPath = "ftp://myftp/";
string downloadFileName = @"C:\temp\FTPTest\";
downloadFileName += "\";
conn.Host = ftpPath;
//conn.Credentials = new NetworkCredential("ftptest", "ftptest");
conn.Connect();
//Get all directories
foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
FtpListOption.Modify | FtpListOption.Recursive))
{
// if this is a file
if (item.Type == FtpFileSystemObjectType.File)
{
string localFilePath = downloadFileName + item.FullName;
//Only newly created files will be downloaded.
if (!File.Exists(localFilePath))
{
conn.DownloadFile(localFilePath, item.FullName);
//Do any action here.
Console.WriteLine(item.FullName);
}
}
}
}
}