[Question] Does Session::RemoveFiles()
remove files in sub directory of source directory? If not, how to implement this ability?
(Please do not ask me why I have the remote directory as /C/testTransfer/
. The code just for testing purpose.)
I have a SFTP program using WinSCP .Net assembly. Program language is C++/CLI. It opens up a work file. The file contains many lines of FTP instructions.
One type of instruction I have to handle is to transfer *.txt
from source directory. The source directory may contain sub directories which may contain .txt
as well. Once transfer is successful, delete the source files.
I use Session::GetFiles()
for the transfer. It correctly transfer all .txt
files (/C/testTransfer/*.txt
), even those in sub directories (/C/testTransfer/sub/*.txt
), in the source to the destination.
transferOptions->FileMask = "*.txt";
session->GetFiles("/C/testTransfer", "C:\\temp\\win", false, transferOption);
Now to remove, I use session->RemoveFiles("/C/testTransfer/*.txt")
. I only see *.txt
in the source (/C/testTransfer/*.txt
), but not in the sub directory (/C/testTransfer/sub/*.txt
), are removed.
The Session::RemoveFiles
can remove even files in subdirectories in general. But not this way with wildcard, because WinSCP will not descend to subdirectories that do not match the wildcard (*.txt
). Also note that even if you do not need the wildcard, the Session::RemoveFiles
would remove even the subdirectories themselves, what I'm not sure you want it to.
Though you have other (and better = more safe) options:
Use the remove
parameter of the Session::GetFiles
method to instruct it to remove source file after successful transfer.
If you need to delete source files transactionally (=only after download of all files succeed), iterate the TransferOperationResult::Transfers
returned by Session::GetFiles
and call the Session::RemoveFiles
for each (unless the TransferEventArgs::Error
is not null).
Use the TransferEventArgs::FileName
to get a file path to pass to the Session::RemoveFiles
. Use the RemotePath::EscapeFileMask
to escape the file name before passing it to the Session::RemoveFiles
.
There's a similar full example available for Moving local files to different location after successful upload.
To recursively delete files matching a wildcard in a standalone operation (not after downloading the same files), use the Session::EnumerateRemoteFiles
. Pass your wildcard to its mask
argument. Use the EnumerationOptions.AllDirectories
option for recursion.
Call the Session::RemoveFiles
for each returned file. Use the RemotePath::EscapeFileMask
to escape the file name before passing it to the Session::RemoveFiles
.