Topic "How to count folders recursively"

Author Message
ssAshkea
[View user's profile]

Joined: 2017-07-22
Posts: 8
Location: uSaka
Already the whole day I am excruciated I can not understand as to count kol-in folders of all.
Recursively

Code:
// 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

Code:
 WinSCP.Session+<DoEnumerateRemoteFiles>d__73
Advertisements
Guest




Who will tell?
martin◆
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26911
Location: Prague, Czechia
I do not know what is "kol-in folders", but this may help:

Code:
int directoriesCount =
    session.EnumerateRemoteFiles(directory, null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories)
        .Where(file => file.IsDirectory)
        .Count();
ssAshkea
[View user's profile]

Joined: 2017-07-22
Posts: 8
Location: uSaka
[quote="Anonymous"]
martin wrote:
I do not know what is "kol-in folders", but this may help:

Code:
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
ssAshkea
[View user's profile]

Joined: 2017-07-22
Posts: 8
Location: uSaka
martin wrote:
I do not know what is "kol-in folders", but this may help:

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



Your method does not help produce an error

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


what to do?
ssAshkea
[View user's profile]

Joined: 2017-07-22
Posts: 8
Location: uSaka
What can you advise me?
And how correctly to specify a directory the absolute way is correct?
Example:
/home/user/
martin◆
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26911
Location: Prague, Czechia
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/")
ssAshkea
[View user's profile]

Joined: 2017-07-22
Posts: 8
Location: uSaka
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

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 ?
martin◆
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26911
Location: Prague, Czechia
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)
ssAshkea
[View user's profile]

Joined: 2017-07-22
Posts: 8
Location: uSaka
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?
martin◆
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26911
Location: Prague, Czechia
ssAshkea wrote:
1) all the files put in the list of recursive?

I do not understand.
ssAshkea
[View user's profile]

Joined: 2017-07-22
Posts: 8
Location: uSaka
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
martin◆
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26911
Location: Prague, Czechia
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:

Code:
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);
ssAshkea
[View user's profile]

Joined: 2017-07-22
Posts: 8
Location: uSaka
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:

Code:
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
martin◆
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26911
Location: Prague, Czechia
OK, so does the code in my previous post help?
Advertisements

You can post new topics in this forum

Search

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License