How to count folders recursively

Advertisement

ssAshkea
Joined:
Posts:
8
Location:
uSaka

How to count folders recursively

Already the whole day I am excruciated I can not understand as to count kol-in folders of all.
Recursively

// Connect
    session.Open(sessionOptions);
   var directory  = session.ListDirectory("/admin");
   var filesCount = session.EnumerateRemoteFiles(directory.ToString(), null, EnumerationOptions.AllDirectories);
   return fileCount ;

Such code outputs such a message to the log

 WinSCP.Session+<DoEnumerateRemoteFiles>d__73

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,276
Location:
Prague, Czechia

Re: How to count folders recursively

I do not know what is "kol-in folders", but this may help:

int directoriesCount =
    session.EnumerateRemoteFiles(directory, null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories)
        .Where(file => file.IsDirectory)
        .Count();

Reply with quote

ssAshkea
Joined:
Posts:
8
Location:
uSaka

Re: How to count folders recursively

[quote="Anonymous"]

martin wrote:

I do not know what is "kol-in folders", but this may help:

int directoriesCount =
    session.EnumerateRemoteFiles(directory, null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories)
        .Where(file => file.IsDirectory)
        .Count();


I apologize, I wanted to write the number of folders and files
I'll try your code, the library you have is very powerful, only my weak knowledge in C# code does not allow to open up all the potential

Reply with quote

ssAshkea
Joined:
Posts:
8
Location:
uSaka

Re: How to count folders recursively

martin wrote:

I do not know what is "kol-in folders", but this may help:

int directoriesCount =
    session.EnumerateRemoteFiles(directory, null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories)
        .Where(file => file.IsDirectory)
        .Count();


Your method does not help produce an error

Error listing directory 'WinSCP.RemoteDirectoryInfo'.
No such file or directory.
Error code: 2
Error message from server: No such file

what to do?

Reply with quote

Advertisement

ssAshkea
Joined:
Posts:
8
Location:
uSaka

ssh

What can you advise me?
And how correctly to specify a directory the absolute way is correct?
Example:
/home/user/

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,276
Location:
Prague, Czechia

Re: How to count folders recursively

OK, I've missed that the directory is result of session.ListDirectory is your code.

You have to pass a path as a string to the first argument of session.EnumerateRemoteFiles.

Like: session.EnumerateRemoteFiles("/home/user/")

Reply with quote

ssAshkea
Joined:
Posts:
8
Location:
uSaka

Re: How to count folders recursively

martin wrote:

OK, I've missed that the directory is result of session.ListDirectory is your code.

You have to pass a path as a string to the first argument of session.EnumerateRemoteFiles.

Like: session.EnumerateRemoteFiles("/home/user/")

And again an error
  No overload for method 'EnumerateRemoteFiles' takes 1 arguments. "[String: 16; Column: 19]

Now I have this code

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.sftp,
    HostName = "server.com",
    UserName = "user",
    Password = "pass",
     SshHostKeyFingerprint = "ssh-rsa 2048 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:1f"
};

using (Session session = new Session())
{
    
    // Connect
    session.Open(sessionOptions);
   var directory  = session.ListDirectory("/home/user/);
   //var filesCount = session.EnumerateRemoteFiles(directory.ToString(), null, EnumerationOptions.AllDirectories);
   //var directoriesCount = session.EnumerateRemoteFiles(directory.ToString(), null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories).Where(file => file.IsDirectory);
   
   return filesCount.Count()  ;
 
   session.Close();
}

what is wrong ?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,276
Location:
Prague, Czechia

Re: How to count folders recursively

ssAshkea wrote:

And again an error
  No overload for method 'EnumerateRemoteFiles' takes 1 arguments. "[String: 16; Column: 19]

OK, so like this: session.EnumerateRemoteFiles("/home/user/", null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories)

Reply with quote

Advertisement

ssAshkea
Joined:
Posts:
8
Location:
uSaka

Re: How to count folders recursively

martin wrote:

ssAshkea wrote:

And again an error
  No overload for method 'EnumerateRemoteFiles' takes 1 arguments. "[String: 16; Column: 19]

OK, so like this: session.EnumerateRemoteFiles("/home/user/", null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories)

Helped work.
And now how do I
1) all the files put in the list of recursive?

Reply with quote

martin
Site Admin
martin avatar

Re: How to count folders recursively

ssAshkea wrote:

1) all the files put in the list of recursive?
I do not understand.

Reply with quote

ssAshkea
Joined:
Posts:
8
Location:
uSaka

Re: How to count folders recursively

martin wrote:

ssAshkea wrote:

1) all the files put in the list of recursive?
I do not understand.

I want to get all the files and folders and put them in a file

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,276
Location:
Prague, Czechia

Re: How to count folders recursively

ssAshkea wrote:

I want to get all the files and folders and put them in a file
Their names/paths or contents? Please be clear!

If paths, this will do:

var filesAndDirectories = session.EnumerateRemoteFiles(directory, null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories);
string list = string.Join(string.Empty, filesAndDirectories.Select(file => file.FullName + Environment.NewLine));
File.WriteAllText(outputFile, list);

Reply with quote

Advertisement

ssAshkea
Joined:
Posts:
8
Location:
uSaka

Re: How to count folders recursively

martin wrote:

ssAshkea wrote:

I want to get all the files and folders and put them in a file
Their names/paths or contents? Please be clear!

If paths, this will do:

var filesAndDirectories = session.EnumerateRemoteFiles(directory, null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories);
string list = string.Join(string.Empty, filesAndDirectories.Select(file => file.FullName + Environment.NewLine));
File.WriteAllText(outputFile, list);

Yes i want to get names and ways of an example:
/home/site.com/file1
/home/site.com/file2
/home/site.com/file3

Reply with quote

Advertisement

You can post new topics in this forum