Search code examples
c#.netwinscpwinscp-net

Remove files (not directories) after GetFiles with WinSCP .NET assembly


I'm trying to download files from a big tree of recursive directories, and I want the downloaded files to be removed after download. When I put true in remove parameter of GetFiles, it removes all the directories, but I want it to remove only the files and leave the directories empty. Is there any way to do it? Thank you.


Solution

  • Check the code of WinSCP extension Recursively move files in directory tree to/from SFTP/FTP server while preserving source directory structure.


    Some alternatives:


    A more complicated, but more efficient (if you have a large amount of files) approach would be:

    • Replicate the directory structure in a remote temporary folder.
    • Move all the files there, directory by directory, using Session.MoveFile. Despite the name, the method accepts wildcards.
    • Download and delete the temporary tree at once using Session.GetFiles with remove set to true.