[英]Counting SFTP files matching certain criteria using SSH.NET
I have a working program that deletes log files from a remote server (based on a certain pattern).我有一个工作程序,可以从远程服务器中删除日志文件(基于某种模式)。 Now when I want to get the count of files that match my criteria I am getting problems.现在,当我想获得符合我的标准的文件数时,我遇到了问题。 It turns I cannot directly get the count from the SftpFile
file object.结果我无法直接从SftpFile
文件 object 中获取计数。 I can only get the count of the files after setting a breakpoint.我只能在设置断点后获取文件的数量。
I am able to delete the files using:我可以使用以下方法删除文件:
private void ListDirectory(SftpClient client, String dirName)
{
var fileext = ".log";
var fileextension = fileext.ToString();
foreach (SftpFile file in client.ListDirectory(dirName))
{
var logFilePath = file.FullName;
var fileCount = client.ListDirectory(dirName).GetEnumerator();
if ((file.Name != ".") && (file.Name != "..") && file.Name.EndsWith(fileextension))
{
Console.WriteLine(file.FullName);
client.Delete(logFilePath);
Console.ReadKey();
}
}
}
And when I do set a breakpoint I can get the count from a nested object of this line:当我设置断点时,我可以从这一行的嵌套 object 中获取计数:
var fileCount = client.ListDirectory(dirName).GetEnumerator();
I have a snapshot of the debug:我有调试的快照:
Now I need a way to directly access the count of files for my pattern ie this line:现在我需要一种方法来直接访问我的模式的文件数,即这一行:
if ((file.Name != ".") && (file.Name != "..") && file.Name.EndsWith(fileextension))
When I try to apply some Linq as below:当我尝试如下应用一些 Linq 时:
var fileCount = client.ListDirectory(dirName).Where((file.Name != ".") && (file.Name != "..") && file.Name.EndsWith(fileextension)).Count();
I get further exception saying我得到进一步的例外说
Cannot convert from 'bool' to 'system.func无法从“布尔”转换为“system.func”
The syntax you have in Where
method argument is not a valid lambda function, you miss the parameter list.您在Where
方法参数中的语法不是有效的 lambda function,您错过了参数列表。 It should be:它应该是:
.Where(file => (file.Name != ".") && (file.Name != "..") && file.Name.EndsWith(fileextension))
Also, do not call ListDirectory
repeatedly, let only in every iteration.另外,不要重复调用ListDirectory
,只在每次迭代中调用。
var files = client.ListDirectory(dirName);
files = files.Where(file => (file.Name != ".") && (file.Name != "..") && file.Name.EndsWith(fileextension));
int count = files.Count();
foreach (SftpFile file in files)
{
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.