批处理文件从FTP服务器下载最新的文件 [英] Batch file to download the latest file from FTP server

查看:174
本文介绍了批处理文件从FTP服务器下载最新的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,可以从我的网络服务器获取FTP CSV文件。
我只需要下载最新的CSV文件。



我该怎么做?



这是我到目前为止:

 打开44.44。 44.444 
用户名
密码

CD /客户端/ ABCCompany /

获取* .csv

退出
close()

谢谢。

解决方案

使用 ftp.exe

  • 如果您知道该文件的文件名中包含当前的时间戳,则可以使用今天的动态脚本动态生成脚本时间戳。您可以使用 DATE 环境变量,尽管它有一些注意事项。一个更可靠的方法是使用 wmic os get LocalDateTime



    请参阅如何在Windows命令行上获取当前日期时间,以适合格式使用的文件名?




    • 运行如果您可以按字母顺序确定最新的文件, ftp.exe ls 命令重定向到文件
    • 按字母顺序对文件进行降序排序

    • 阅读第一行
    • 生成第二个 ftp.exe 运行的下载脚本。 li>






    • WinSCP 可以使用 -latest 下载最新的文件,切换 获取命令(WinSCP 5.9和更新版本):

        winscp.com / command ^ 
      open ftp:// username:password@ftp.example.com/^
      cd / remote / path^
      get -latest * .csv^
      exit

      另见指南使用WinSCP下载最新文件


    • WinSCP还可以在一段时间内下载创建的文件,例如最后24小时(1天):

        winscp.com /命令^ 
      open ftp://用户名:password@ftp.example.com/^
      cd / remote / path^
      get * .csv> = 1D^
      exit

      或某些时间点后创建的文件,例如从午夜开始,使用 %TIMESTAMP%语法

        winscp.com /命令^ 
      open ftp:// username:password @ ftp .example.com /^
      cd / remote / path^
      get * .csv> = %% TIMESTAMP#yyyy-mm-dd %%^
      exit


    • 使用WinSCP,您可以更轻松地实现带有时间戳的文件下载与 DATE wmic os获得LocalDateTime (如上所示)相比。再次使用%TIMESTAMP%语法:

        winscp.com /命令^ 
      open ftp:// username:password@ftp.example.com/^
      cd / remote / path^
      get %% TIMESTAMP#yyyy-mm-dd %%。txt^
      exit

      (我是WinSCP的作者)



    I have a batch file that FTPs CSV files from my web server. I need to download only the most current CSV file.

    How do I do that?

    This is what I have so far:

    open 44.44.44.444
    username
    password
    
    CD /Client/ABCCompany/
    
    get *.csv
    
    quit
    close()
    

    Thanks.

    解决方案

    There's no easy way to select the most recent file with the ftp.exe.

    • If you know that the file has today's timestamp in its filename, you can generate the script dynamically with today's timestamp. You can use the DATE environment variable, though it has its caveats. A more reliable (and complex) way is to use the wmic os get LocalDateTime.

      See How to get current datetime on Windows command line, in a suitable format for using in a filename?

    • If you can determine the latest file alphabetically, you can:

      • run the ftp.exe with ls command redirected to a file
      • sort the file by alphabet in descending order
      • read the first line
      • generate download script for second ftp.exe run.
    • WinSCP can download the latest file, using the -latest switch of the get command (WinSCP 5.9 and newer only):

      winscp.com /command ^
          "open ftp://username:password@ftp.example.com/" ^
          "cd /remote/path" ^
          "get -latest *.csv" ^
          "exit"
      

      See also the guide to downloading the most recent file with WinSCP.

    • WinSCP can also download the files created within some interval, e.g. the last 24-hours (1 day):

      winscp.com /command ^
          "open ftp://username:password@ftp.example.com/" ^
          "cd /remote/path" ^
          "get *.csv>=1D" ^
          "exit"
      

      or files created since some point in time, e.g. since midnight, using the %TIMESTAMP% syntax:

      winscp.com /command ^
          "open ftp://username:password@ftp.example.com/" ^
          "cd /remote/path" ^
          "get *.csv>=%%TIMESTAMP#yyyy-mm-dd%%" ^
          "exit"
      

    • With WinSCP, you can implement a download of a file with a timestamp in filename more easily than with the DATE or the wmic os get LocalDateTime (as shown above). Again use the %TIMESTAMP% syntax:

      winscp.com /command ^
          "open ftp://username:password@ftp.example.com/" ^
          "cd /remote/path" ^
          "get %%TIMESTAMP#yyyy-mm-dd%%.txt" ^
          "exit"
      

      (I'm the author of WinSCP)

    这篇关于批处理文件从FTP服务器下载最新的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆