我有一个批处理文件,可以从我的Web服务器FTP CSV文件。
我需要下载最新的CSV文件。
我该如何做?
这是我目前所拥有的:
open 44.44.44.444
username
password
CD /Client/ABCCompany/
get *.csv
quit
close()
谢谢。
我有一个批处理文件,可以从我的Web服务器FTP CSV文件。
我需要下载最新的CSV文件。
我该如何做?
这是我目前所拥有的:
open 44.44.44.444
username
password
CD /Client/ABCCompany/
get *.csv
quit
close()
谢谢。
使用ftp.exe
无法轻松选择最新的文件。
如果您知道文件名中包含今天的时间戳,可以动态生成带有今天时间戳的脚本。您可以使用DATE
环境变量,但它有其注意事项。更可靠(但更复杂)的方法是使用wmic os get LocalDateTime
。
如果您可以按字母顺序确定最新的文件,则可以:
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)
最简单的方法是将其拆分为两个独立的连接,并在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()
我经常需要从我们的生产环境下载备份文件,以便在开发环境中安装。
在我们的情况下,我想要自动下载的文件名中包含日期: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%"
你很可能需要分两个步骤进行传输。第一个步骤是发出 DIR 命令,最新的文件应该在最后。然后从 DIR 输出中解析出最后一个文件名,并将该文件名用于后续的 GET。
我在 C# 中做类似的事情。