【问题标题】:How to download 10 newest files of type CSV via SFTP using WinSCP?如何使用 WinSCP 通过 SFTP 下载 10 个最新的 CSV 类型文件?
【发布时间】:2018-05-21 18:55:33
【问题描述】:

我基本上从 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 如何获取最新文件。而且我发现使用智能感知有一个RemoteFileInfoCollection 类。

但是这个类没有很好的文档记录(或者至少不够好让我使用)

问题:

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

【问题讨论】:

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


    【解决方案1】:

    使用代码you have found to download the one latest file 并将FirstOrDefault 替换为Take 并迭代该集合以下载所有选定的文件。

    我也在使用EnumerateRemoteFiles 而不是ListDirectory,因为它可以自己按文件掩码过滤文件。

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 2015-09-08
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多