简体   繁体   English

使用WinSCP .NET程序集仅使用SFTP协议删除或移动所选文件

[英]Remove or move only selected files with SFTP protocol using WinSCP .NET assembly

I'm trying to develop a simple SFTP file transferring project with following operations 我正在尝试使用以下操作开发一个简单的SFTP文件传输项目

  1. Upload 上传
  2. Download 下载
  3. Move files within the remote server 移动远程服务器中的文件
  4. Remove files 删除文件

While uploading in session.PutFiles() we have a property called transferOptions.FileMask to filter the files. session.PutFiles()上传时,我们有一个名为transferOptions.FileMask的属性来过滤文件。

But I didn't see anything of that kind in session.MoveFile() and in session.RemoveFiles() 但我在session.MoveFile()session.RemoveFiles()没有看到任何类似的东西。

My question is what should I do if I need to move/remove only selected files? 我的问题是,如果我需要移动/删除所选文件,我该怎么办?

The Session.RemoveFiles accepts a file mask . Session.RemoveFiles接受文件掩码

So you can do: 所以你可以这样做:

session.RemoveFiles("/home/user/*.txt");

It's the same as with the Session.PutFiles . 它与Session.PutFiles相同。 The TransferOptions.FileMask is actually intended for advanced selections, like when you want to select files recursively, or when you want to exclude certain types of files. TransferOptions.FileMask实际上用于高级选择,例如,当您想要递归选择文件时,或者想要排除某些类型的文件时。

session.PutFiles(@"c:\toupload\*.txt", "/home/user/");

With the TransferOption.FileMask , WinSCP would upload all matching files recursively. 使用TransferOption.FileMask ,WinSCP将递归上传所有匹配的文件。 While with a simple file mask as the argument to the .PutFiles , it's not recursive. 虽然使用简单的文件掩码作为.PutFiles的参数,但它不是递归的。


The Session.MoveFile actually supports the file mask in its first argument too, though it's an undocumented feature. Session.MoveFile实际上也支持第一个参数中的文件掩码, 尽管它是一个未记录的功能。

A proper way would be to list remote directory, select desired files and call the Session.MoveFile for each. 一种正确的方法是列出远程目录,选择所需的文件并为每个文件调用Session.MoveFile

See Listing files matching wildcard . 请参阅列出匹配通配符的文件 It's a PowerShell example, but mapping it to C# should be easy. 这是一个PowerShell示例,但将其映射到C#应该很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM