批处理文件下载FTP服务器上的最新文件

6

我有一个批处理文件,可以从我的Web服务器FTP CSV文件。

我需要下载最新的CSV文件。

我该如何做?

这是我目前所拥有的:

open 44.44.44.444
username
password

CD /Client/ABCCompany/

get *.csv

quit
close()

谢谢。


你如何知道哪一个是最新的?是根据文件格式还是文件创建日期? - ablm
我不熟悉FTP命令的脚本编写,这是我更新的内容,但出现了“无效命令”错误:**FOR /F %%I IN ('DIR .csv /B /O:-D') DO COPY %%I c:* - Stan
/B和/O是做什么用的?我在其他Forfile帖子中没有看到这些被提到过。 - Stan
@GregHewgill,感谢您指向批处理脚本页面。我不太理解Keys以及如何使用它们。由于这是FTP,所以没有复制命令,对吗? - Stan
既然这是一个Web服务器,你必须使用FTP吗?对于类似的问题,我使用wget发出HTTP请求以将目录索引下载为HTML文件。然后在本地处理索引以查看哪个是最新的文件(我使用awk进行此操作),然后发出另一个HTTP请求以获取该文件。 - lessthanideal
4个回答

6

使用ftp.exe无法轻松选择最新的文件。

  • 如果您知道文件名中包含今天的时间戳,可以动态生成带有今天时间戳的脚本。您可以使用DATE环境变量,但它有其注意事项。更可靠(但更复杂)的方法是使用wmic os get LocalDateTime

    请参见如何在Windows命令行上获取当前日期/时间,以适合用于文件/文件夹名称?

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

    • 运行带有重定向到文件的ls命令的ftp.exe
    • 按字母顺序降序排序文件
    • 读取第一行
    • 为第二个ftp.exe运行生成下载脚本。
  • WinSCP can download the latest file, using the -latest switch of the get command:

    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. to download files created since midnight, use today keyword:

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

    For the today keyword, make sure you have recent version of WinSCP.

  • 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). 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)


0

最简单的方法是将其拆分为两个独立的连接,并在FTP位置上放置一个文本文件,其中包含最新文件的名称。

open 44.44.44.444
username
password
CD /Client/ABCCompany/
get latestfile.txt
quit
close()

latestfile.txt 文件将包含您需要下载的最新文件的名称。 第二个脚本将从 latestfile.txt 中读取文本并仅拉取该文件。

for /F "tokens=*" %%A in (latestfile.txt) do [SET FILE = %%A] 

您需要将上述行添加到调用次要FTP脚本的批处理文件中。

open 44.44.44.444
username
password
CD /Client/ABCCompany/
get %FILE%
quit
close()

0

我经常需要从我们的生产环境下载备份文件,以便在开发环境中安装。

在我们的情况下,我想要自动下载的文件名中包含日期:backup_2018_08_03_020003_1048387.bak

因此,我们可以在命令行ftp会话中使用mget *2018_08_03*来获取文件。

我们的备份程序每天早上01:00运行一次,因此我们每天都可以获取备份。

当然,如果最新的备份出现问题或备份文件命名格式发生更改,基于备份文件时间戳获取最新备份文件的脚本会更美观和更好。该脚本只是为了内部开发目的而编写的备份脚本,因此如果它出现故障也不是很重要。稍后我将研究这个问题并检查是否可以提供更简洁的解决方案。

我制作了一个批处理脚本,它只需使用普通的ftp命令提示符脚本即可请求今天的备份文件。

正确获取今天日期的格式非常重要。它必须正确匹配文件名中的日期格式。

如果你想使用这个脚本,你需要用自己的信息替换变量。你还应该拥有运行脚本所在目录的写入访问权限。

这是我制作的脚本:

@Echo Off
Set _FTPServerName=xxx.xxx.xx.xxx
Set _UserName=Username
Set _Password=Password
Set _LocalFolder=C:\Temp
Set _RemoteFolder="/path/"
Set _Filename=*%date:~-4,4%_%date:~-7,2%_%date:~-10,2%*
Set _ScriptFile=ftptempscript
:: Create script
 >"%_ScriptFile%" Echo open %_FTPServerName%
>>"%_ScriptFile%" Echo %_UserName%
>>"%_ScriptFile%" Echo %_Password%
>>"%_ScriptFile%" Echo lcd %_LocalFolder%
>>"%_ScriptFile%" Echo cd %_RemoteFolder%
>>"%_ScriptFile%" Echo binary
>>"%_ScriptFile%" Echo mget -i %_Filename%
>>"%_ScriptFile%" Echo quit
:: Run script
ftp -s:"%_ScriptFile%"
del "%_ScriptFile%"

0

你很可能需要分两个步骤进行传输。第一个步骤是发出 DIR 命令,最新的文件应该在最后。然后从 DIR 输出中解析出最后一个文件名,并将该文件名用于后续的 GET。

我在 C# 中做类似的事情。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接