Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 468
user5441417 Avatar asked Sep 06 '25 05:09

user5441417


1 Answers

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


Some alternatives:

  • Do a regular download = do not set remove parameter of Session.GetFiles to true.
  • Iterate TransferOperationResult.Transfers returned by the Session.GetFiles.
  • For each successful download of a file, call Session.RemoveFiles.

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.
like image 120
Martin Prikryl Avatar answered Sep 09 '25 01:09

Martin Prikryl