3

I'm trying to get the files count from a remote directory using a SFTP connection, but I'm getting . and .. and these are counted these dots like a files, I have 2 files in the remote directory but is counting 4 files including . and ...

Someone can help me how to solve this?

This is my code:

filesCount = session.ListDirectory(DataFile.sRemoteDirectory).Files.Count;                

Thanks!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Javier Salas
  • 1,064
  • 5
  • 15
  • 38

5 Answers5

5

According to the WinSCP documentation:

You can use Session.EnumerateRemoteFiles method instead, if you want to:

  • List only files matching a wildcard;
  • List the files recursively;
  • Have references to this (.) and parent (..) directories be excluded form the listing.

So it appears that you should change your code to do something more like this:

filesCount = 0; 
filesCount = session.EnumerateRemoteFiles(DataFile.sRemoteDirectory).Files.Count();                
session.Close();
Community
  • 1
  • 1
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
3

Instead of using ListDirectory you can use EnumerateRemoteFiles and it wont include ".." and "."

"." and ".." mean this directory and parent directory respectively.

Lewis Taylor
  • 673
  • 3
  • 13
2

The . and .. are references to this and parent directories respectively on most file systems.


To filter them, you can use new properties .IsThisDirectory and .IsParentDirectory of the RemoteFileInfo class:

filesCount =   
    session.ListDirectory(DataFile.sRemoteDirectory).Files
        .Where(file => !file.IsThisDirectory && !file.IsParentDirectory).Count();

Note that you have to use the Enumerable.Count() extension method, instead of the ICollection.Count property as the result of the Enumerable.Where() is the IEnumerable, not the Collection anymore.


Or to make it even easier, use the Session.EnumerateRemoteFiles() method, which with the EnumerationOptions.None option is functionally equivalent to the Session.ListDirectory(), just that it excludes the . and ...

filesCount =
    session.EnumerateRemoteFiles(
        DataFile.sRemoteDirectory, null, EnumerationOptions.None).Count();

If you want to filter all directories, use:

filesCount =   
    session.ListDirectory(DataFile.sRemoteDirectory).Files
        .Where(file => !file.IsDirectory).Count();               
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

Try session.EnumerateRemoteFiles instead.

Richard Vasquez
  • 187
  • 1
  • 5
0

Despite its naming, Files collection contains not only files, but all directory entries, including current and parent directory references.

If you want to count only files, filter them by IsDirectory property:

var filesCount = session.ListDirectory(dir).Files.Where(x => !x.IsDirectory).Count();

There's also IsParentDirectory and IsThisDirectory properties in the latest versions to filter ".." and "." cases without name comparison.

Alex Skalozub
  • 2,511
  • 16
  • 15
  • This excludes all directories, not just `.` and `..`. So while it works, if there are no other directories, it does not do exactly what the the question is asking for. – Martin Prikryl Dec 30 '15 at 17:09