批处理/宏代码以从FTP站点获取最新文件 [英] Batch/Macro code to get latest file from FTP site

查看:88
本文介绍了批处理/宏代码以从FTP站点获取最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个共享的FTP站点,每天上传文件,我需要下载该文件并进行检查.我只是想使过程自动化.FTP文件类似于 name_20150901.xml .

We have a shared FTP site with a daily file upload which I need to download and run checks over. I'm simply trying to automate the process. The FTP file is something like name_20150901.xml.

到目前为止,我有一个批处理文件来获取文件,但是我不知道如何获取最新文件.这是我的代码:

So far I have a batch file to get the file but I can't figure out how to get the latest. Here is my code:

@ftp -i -s:"%~f0"&GOTO:EOF
open ftp.site.com
<user>
<pass>
lcd my local direc
binary
get todaysfile_20150901.xml

要读取文件并获取最新文件,我需要进行哪些更改?我的最终目标是拥有一个调用此宏的宏,然后读取从FTP抓取的文件并运行我的检查.

What changes do I need to read the files and get the newest one? My end goal is to have a macro that calls this and then reads the file that's grabbed from the FTP and runs my checks.

推荐答案

没有简单的方法通过 ftp.exe 选择最新文件.

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

  • 如果您知道文件具有今天的时间戳,则可以使用今天的时间戳动态生成脚本.您可以使用 DATE 环境变量,尽管它有一些警告.一种更可靠(更复杂)的方法是使用 wmic os get LocalDateTime .

  • If you know that the file has today's timestamp, you can generate the script dynamically with the 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.

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

如果可以按字母顺序确定最新文件,则可以:

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

  • 使用 ls 命令重定向到文件运行 ftp.exe
  • 按字母降序对文件进行排序
  • 阅读第一行
  • 为第二个 ftp.exe 运行生成下载脚本
  • run the ftp.exe with the ls command redirected to a file
  • sort the files by alphabet in a descending order
  • read the first line
  • generate a download script for the second ftp.exe run
  • WinSCP 可以下载最近24小时(1天)内创建的文件:

  • WinSCP can download the files created within the last 24-hours (1 day):

winscp.com /command ^
    "open ftp://user:pass@ftp.site.com/" ^
    "lcd c:\my\local\direc" ^
    "get *>=1D" ^
    "exit"

  • 如果确实需要下载最新文件(按时间戳记),则可能需要一些更高级的脚本.

  • If you really need to download the latest file (by timestamp), you probably need some more advanced scripting.

    例如,有一个指南可供您使用WinSCP下载最新文件.

    For example there's a guide available for downloading the most recent file with WinSCP.

    (我是WinSCP的作者)

    这篇关于批处理/宏代码以从FTP站点获取最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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