Windows批处理脚本下载昨天的文件
我正在编写一个使用 ftp.exe 来从 FTP 服务器下载文件的脚本,它首先可以工作。但我写的版本只适合一个文件和当前日期。我的脚本如下:
echo user>>ftp.txt
echo password>>ftp.txt
set prefix=%date:~0,10%
set "name=%prefix%.txt"
echo get %name% >> ftp.txt
echo bye >> ftp.txt
ftp -s:ftp.txt ftpserver.com
del ftp.txt
但是现在有多个名为 aa-bb-2011-09-13.0.log
的文件, aa-bb-2011-09-13.1.log
, aa-bb-2011-09-13.10.log
。最后一个数字是序列号,可以是0
、1
、2
、3
...
如何可以通过批处理脚本下载这些文件吗?如何修改我的脚本以下载多个文件(数量未知)哪个文件名模式是昨天?
I am writing a script using ftp.exe
to download files from an FTP server, it works at first. But the version I wrote was suited for only one file and the current date. My script is below:
echo user>>ftp.txt
echo password>>ftp.txt
set prefix=%date:~0,10%
set "name=%prefix%.txt"
echo get %name% >> ftp.txt
echo bye >> ftp.txt
ftp -s:ftp.txt ftpserver.com
del ftp.txt
But now there are more than one file named like aa-bb-2011-09-13.0.log
,aa-bb-2011-09-13.1.log
,aa-bb-2011-09-13.10.log
. The last number is a serial number, it could be 0
, 1
, 2
, 3
...
How could download these files by batch script? How to modify my script to download more than one file (the number is unknown) which file name pattern is yesterday?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在下载多个文件方面,使用
mget
而不是get
。前者允许您指定获取通配符而不是特定文件。您只需使用通配符模式构造“名称”,并确保脚本中的
mget
之前有一个prompt
,否则它将要求对每个文件。这尚未经过测试,但可能很简单,只需将:更改
为:
就获取昨天的日期而言,您可以使用以下脚本。与您在 bash 等中执行的操作相比,它相当复杂,但它确实有效。
它将把
yesterday
环境变量设置为YYYY-MM-DD
格式,以便您可以在当前脚本中使用它。只需调用call Tuesday.cmd
然后使用环境变量即可。In terms of downloading multiple files, use
mget
instead ofget
. The former allows you to specify wildcards for getting rather than specific files.You'll just have to construct the "name" with a wildcard pattern, and make sure you have a
prompt
in your script beforemget
otherwise it will ask for confirmation on every file.This is untested, but it's probably as simple as changing:
to something like:
In terms of getting yesterdays date, you can use the following script. It's pretty complex compared to what you would do in, for example
bash
, but it works.It will set the
yesterday
environment variable to the formatYYYY-MM-DD
so that you can use it in your current script. Simply invokecall yesterday.cmd
and then use the environment variable.使用 Windows 批处理文件和内置 FTP 客户端 (
ftp.exe
) 来实现这是一项相当复杂的任务。使用 PowerShell 会更容易。
使用功能更强大的应用程序甚至更容易,例如 WinSCP FTP 客户端。
如果您想根据文件名中的模式下载文件,可以这样做:
这使用
%TIMESTAMP%
语法。如果您想根据文件修改时间进行下载,请使用具有时间限制的文件掩码< /a>:
(我是 WinSCP 的作者)
It's a pretty complex task to implement with Windows batch-file and the built-in FTP client (
ftp.exe
).It would be more easier with PowerShell.
And even easier using a more capable application, like WinSCP FTP client.
If you want to download files based on a pattern in a file name, this will do:
This uses the
%TIMESTAMP%
syntax.If you want to download based on a file modification time, use a file mask with a time-constraint:
(I'm the author of WinSCP)
这是一个示例 FTP 脚本,几乎完全满足您的需要,但它使用第 3 方客户端而不是 Windows 附带的客户端: http://kb.robo-ftp.com/script_library/show/45
也许你可以转换它。
This is a sample FTP script that does almost exactly what you need but it uses a 3rd party client instead of the one that comes free with Windows: http://kb.robo-ftp.com/script_library/show/45
Maybe you can convert it.