2

Have to questions cause can't find it:

  1. How to check if directory contains anything e.g folder or files, whatever... or is it empty

  2. How to remove an empty directory?

  3. How to remove a directory even if there is content.

For instance for creating directory I am using below function:

Public Sub CreateDirectory(path As String)
    If session IsNot Nothing Then
        session.CreateDirectory(path)
    End If
End Sub

Log file as requested in comment:

> 2015-10-05 11:11:13.010 MLST /\MainFolder 2014\ANIA
< 2015-10-05 11:11:13.104 550 MLST command failed: No such file or directory.
. 2015-10-05 11:11:13.104 Could not retrieve file information
< 2015-10-05 11:11:13.104 Script: Can't get attributes of file '\MainFolder 2014\ANIA'.
< 2015-10-05 11:11:13.104 Script: Could not retrieve file information

< 2015-10-05 11:11:13.104 MLST command failed: No such file or directory.
. 2015-10-05 11:11:13.104 Script: Failed
Community
  • 1
  • 1
Arie
  • 3,041
  • 7
  • 32
  • 63

1 Answers1

2

To check, if there are any files in a directory, use the Session.EnumerateRemoteFiles method:

Dim anyFile As Boolean =
    mySession.EnumerateRemoteFiles(
        path, Nothing, EnumerationOptions.MatchDirectories).Any()

The Session.EnumerateRemoteFiles is supported since WinSCP 5.9.


With older versions of WinSCP, list directory contents using the Session.ListDirectory and filter out the .. and . entries:

Dim anyFile As Boolean =
    mySession.ListDirectory(path).Files.
    Where(Function(file) (file.Name <> "..") And (file.Name <> ".")).
    Any()

To remove any directory, be it empty or not, use Session.RemoveFiles:

session.RemoveFiles(RemotePath.EscapeFileMask(path))
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • this one is not working: session.RemoteFiles(session.EscapeFileMask(path)) . This is not deleting folder itself but only fiels within this folder. – Arie Oct 05 '15 at 08:12
  • either sometimes not deleting files as well. – Arie Oct 05 '15 at 08:29
  • 1
    What is the exact value of `path` you are using? – Martin Prikryl Oct 05 '15 at 08:34
  • 1
    Set `session.SessionLogPath` and append the log to your question. – Martin Prikryl Oct 05 '15 at 08:54
  • see main topic - ANIA is the folder which should be deleted no matter if it contains something or not. – Arie Oct 05 '15 at 09:13
  • 1
    You need to use forward slashes, not backslashes. Exactly as you claim to have used in your comment. I.e. `/folder/folderwhichshouldbedeleted`, not `\folder\folderwhichshouldbedeleted`. – Martin Prikryl Oct 05 '15 at 09:22
  • but all other operations i am doing using winscp i am doing using \ and everything is working.. – Arie Oct 05 '15 at 09:23
  • 1
    In same cases, WinSCP might try to guess you made this mistake and replace the backslashes with slashes. But you cannot rely on this. It's simply wrong. – Martin Prikryl Oct 05 '15 at 09:28
  • Now is working. So its right way to delete given directory and its contest correct? Can you also look at my second topic ? : http://stackoverflow.com/questions/32945059/winscp-ftp-get-picture-path – Arie Oct 05 '15 at 09:33
  • *"So its right way to delete given directory and its contest correct?"*: I do not understand what you ask. – Martin Prikryl Oct 05 '15 at 09:40
  • Nothing forget - its working for me that's it. Could you be so kind and take a look at my second post? – Arie Oct 05 '15 at 09:45