如何使用WinSCP通过SFTP下载10个CSV类型的最新文件?

时间:2018-05-21 18:55:33

标签: c# .net sftp winscp winscp-net

我基本上从C#使用WinSCP。例如,我知道我可以使用以下代码从FTP站点下载多个CSV文件:

var remotePath = "some\path*.csv";
var localPath = "some\path";
TransferOperationResult transferResult =
    session.GetFiles(remotePath, localPath, false, transferOptions);

但是,它会从SFTP站点下载所有CSV。我只想要最新的10.我从这个链接看到:https://winscp.net/eng/docs/script_download_most_recent_file如何获取最新文件。我发现使用intellisense有一个RemoteFileInfoCollection类。

但是这个课程没有很好的记录(或者至少不足以让我使用)

问题:

  1. 我该如何使用这个课程?
  2. 我如何要求某些'使用seesion.GetFiles()的SFTP站点上的CSV,因为remotePath参数是字符串而不是列表。我知道我可以遍历路径列表并从FTP下载,这是一种合理的方法吗?我不确定我是否想要多次调用GetFiles(),因为它似乎特意命名为文件,我知道它确实会一次下载多个文件。

1 个答案:

答案 0 :(得分:1)

使用代码obsolete,只需将const string remotePath = "/remote/path"; const string localPath = "C:\local\path"; IEnumerable<RemoteFileInfo> files = session.EnumerateRemoteFiles(remotePath, "*.csv", EnumerationOptions.None) .Where(file => !file.IsDirectory) .OrderByDescending(file => file.LastWriteTime) .Take(10); string destPath = Path.Combine(localPath, "*"); foreach (RemoteFileInfo file in files) { Console.WriteLine("Downloading {0}...", file.Name); session.GetFiles(RemotePath.EscapeFileMask(file.FullName), destPath).Check(); } 替换为you have found to download the one latest file,然后重复设置即可下载所有选定的文件。

我还使用Take代替@Html.TextBoxFor(m => m.Date1, "{0:yyyy-MM-dd}", new { type = "date", @class = "form-control", id = "Date1"}) @Html.TextBoxFor(m => m.Date2, "{0:yyyy-MM-dd}", new { type = "date", @class = "form-control", id = "Date2" }) @Html.CheckBoxFor(m => m.ISChecked, new { @class = "icheck", id = "Checkbox1" }) ,因为它可以通过filemask自行过滤文件。

Uncaught Exception:
Error: spawn node ENOENT
    at exports._errnoException (util.js:1024:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:192:19)
    at onErrorNT (internal/child_process.js:374:16)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:607:11)
    at startup (bootstrap_node.js:167:16)
    at bootstrap_node.js:589:3
相关问题