Re: How to count folders recursively
OK, so does the code in my previous post help?
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);
I want to get all the files and folders and put them in a file
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);
1) all the files put in the list of recursive?
I do not understand.
1) all the files put in the list of recursive?
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)
And again an error
No overload for method 'EnumerateRemoteFiles' takes 1 arguments. "[String: 16; Column: 19]
session.EnumerateRemoteFiles("/home/user/", null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories)
OK, I've missed that thedirectory
is result ofsession.ListDirectory
is your code.
You have to pass a path as a string to the first argument ofsession.EnumerateRemoteFiles
.
Like:session.EnumerateRemoteFiles("/home/user/")
// 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();
}
directory
is result of session.ListDirectory
is your code.
session.EnumerateRemoteFiles
.
session.EnumerateRemoteFiles("/home/user/")
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();
Error listing directory 'WinSCP.RemoteDirectoryInfo'.
No such file or directory.
Error code: 2
Error message from server: No such file
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();
int directoriesCount =
session.EnumerateRemoteFiles(directory, null, EnumerationOptions.AllDirectories | EnumerationOptions.EnumerateDirectories)
.Where(file => file.IsDirectory)
.Count();
// Connect
session.Open(sessionOptions);
var directory = session.ListDirectory("/admin");
var filesCount = session.EnumerateRemoteFiles(directory.ToString(), null, EnumerationOptions.AllDirectories);
return fileCount ;
WinSCP.Session+<DoEnumerateRemoteFiles>d__73