【问题标题】:Get the latest updated file from FTP Folder从 FTP 文件夹获取最新更新的文件
【发布时间】:2014-10-25 12:10:42
【问题描述】:

请观看此屏幕截图以更好地了解我们的要求:

https://www.screenr.com/QmDN

我们希望自动生成文本数据源并连接到 MS Excel,以便最终用户更轻松地将文本数据源 (CSV) 连接到 MS Excel,以便他们生成自己的报告。

我想到的步骤:

  1. 使用带有脚本的 WinSCP FTP 客户端

  2. 编写脚本以从 FTP 文件夹中获取最新更新的文件

  3. 或者代替第 2 步,将所有生成的文件从 FTP 下载到网络上的共享文件夹。

  4. 获取生成的 CSV 文件的最新版本

  5. 将文件重命名为标准命名约定。这必须是 MS Excel 中用作 CSV 文本数据源的名称。

  6. 删除所有其他文件

我开发了 WinSCP 可以用来从 FTP 文件夹下载文件的示例脚本:

# Automatically abort script on errors
option batch abort
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect
open CSOD
# Change remote directory
cd /Reports/CAD
# Force binary mode transfer
option transfer binary
# Download file to the local directory d:\
#get "Training Attendance Data - Tarek_22_10_21_2014_05_05.CSV" "D:\MyData\Business\Talent Management System\Reports\WinCSP\"
get "*.CSV" "D:\MyData\Business\Talent Management System\Reports\WinCSP\Files\"
# Disconnect
close
exit

然后,我可以使用此命令安排上述代码定期运行:

winscp.com /script=example.txt

上面的示例工作正常,但主要问题是如何识别最近的文件,以便我可以重命名它,并删除所有其他文件。

感谢您的帮助。

塔雷克

【问题讨论】:

    标签: csv ftp datasource winscp


    【解决方案1】:

    只需将-latest 开关添加到get command

    get -latest "*.CSV" "D:\MyData\Business\Talent Management System\Reports\WinCSP\Files\"
    

    更多详情,请参阅 WinSCP 文章Downloading the most recent file

    【讨论】:

    • 谢谢!真的很感激。
    • 我刚刚意识到我可以接受你的回复作为答案,虽然有点晚了。
    【解决方案2】:

    您没有指定您使用的语言,这里是下载 FTP 路径的最新文件的 Ruby 脚本。只是为了演示使用像 Ruby 这样的脚本语言可以做到这一点是多么简单和简洁。

    require 'net/ftp'
    
    Net::FTP.open('url of ftpsite') do |ftp|
      ftp.login("username", "password")
      path = "/private/transfer/*.*"
      # file[55..-1] gives the filename part of the returned string
      most_recent_file = ftp.list(path)[2..-1].sort_by {|file|ftp.mtime(file[55..-1])}.reverse.first[55..-1]
      puts "downloading #{most_recent_file}"
      ftp.getbinaryfile(most_recent_file, File.basename(most_recent_file))
      puts "done"
    end
    

    【讨论】:

    • 谢谢。我使用的语言是工具 WinSCP 的脚本:winscp.net/eng/docs/scripting。我不懂 Ruby,这里没有人知道如何用 Ruby 编程。感谢使用批处理命令(Windows 命令行)举例。
    • WinSCP 不是脚本语言,它是一种特定于域的语言(在这方面使 Ruby 易于阅读,因为它具有很强的可读性)。 Ruby 是我所知道的最容易使用的通用脚本语言,它经常用于教孩子们编程。我建议你看看它。我用 Ruby 制作这个工作示例需要几分钟,批量完成需要几个小时,所以很抱歉,我不会这样做
    • 这将使用批处理文件:stackoverflow.com/questions/97371/… FOR /F "delims=|" %%I IN ('DIR "." /B /O:D') DO SET NewestFile=%%I 所以不需要学习一门新的语言有额外的不必要的复杂层。
    • 这是来自 WinSCP 论坛的回复:winscp.net/forum/viewtopic.php?p=53040#53040 ...看来已经有解决方案了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多