Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a working query that deletes log files from a remote server. Now when i want to get the count of files that match my criteria and therefore those to delete i am unable using the
SftpClient client
. I am able to delete files as follows :

C#
<pre> 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();
                }
            }
        }


When i place a breakpoint i can get the file count from the line :
var fileCount = client.ListDirectory(dirName).GetEnumerator();


This is however a nested object of the GetEnumerator method as in the attached picture:
https://drive.google.com/open?id=1wTZT1yPuJZnXyhgrLi8yq1qXS6s5dhjU

What I have tried:

How can i directly get the count of files matching my filter , i.e the number of files here:
if ((file.Name != ".") && (file.Name != "..") && file.Name.EndsWith(fileextension))
Posted
Updated 26-May-20 1:37am

1 solution

Simple way

int count = 0;
if ((file.Name != ".") && (file.Name != "..") && file.Name.EndsWith(fileextension))
{
    Console.WriteLine(file.FullName);
    client.Delete(logFilePath);
    count += 1;
    Console.ReadKey();
}


more complex (needs more work & using ref for LINQ)

var fileCount = client.ListDirectory(dirName).Where((file.Name != ".") && (file.Name != "..") && file.Name.EndsWith(fileextension)).Count();
 
Share this answer
 
Comments
Tshumore 27-May-20 23:15pm    
When i use Linq i am getting error :
Cannot convert from 'bool' to 'system.func<renci.ssh.sftp.sftpfile, bool="">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900