windows Windows批处理脚本下载昨天的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7412623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows batch script to download yesterday files
提问by huangli
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:
我正在编写一个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
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
...
但是现在有不止一个文件名为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
...
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?
如何通过批处理脚本下载这些文件?如何修改我的脚本以下载多个文件(数量未知)昨天哪个文件名模式?
回答by paxdiablo
In terms of downloading multiple files, use mget
instead of get
. The former allows you to specify wildcards for getting rather than specific files.
在下载多个文件方面,使用mget
代替get
. 前者允许您指定通配符来获取而不是特定文件。
You'll just have to construct the "name" with a wildcard pattern, and make sure you have a prompt
in your script before mget
otherwise it will ask for confirmation on every file.
您只需要使用通配符模式构建“名称”,并确保您prompt
的脚本中有一个,mget
否则它会要求对每个文件进行确认。
This is untested, but it's probably as simple as changing:
这是未经测试的,但它可能就像更改一样简单:
echo get %name% >> ftp.txt
to something like:
类似于:
echo prompt>>ftp.txt
echo mget *%prefix%*>>ftp.txt
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.
在获取昨天的日期方面,您可以使用以下脚本。例如bash
,与您将要执行的操作相比,它非常复杂,但它确实有效。
@setlocal enableextensions enabledelayedexpansion
@echo off
rem Get the date from WMI (on one line).
for /f "skip=2 tokens=2-7 delims=," %%A in ('wmic
path win32_localtime get day^,month^,year^ /format:csv') do (
set /a "yest_yyyy = %%C"
set /a "yest_mm = %%B"
set /a "yest_dd = %%A"
)
rem Not the first of the month, just decrement day.
if not %yest_dd%==1 (
set /a yest_dd = yest_dd - 1
goto done
)
rem Jan 1, set to Dec 31 previous year.
if %yest_mm%==1 (
set /a "yest_dd = 31"
set /a "yest_mm = 12"
set /a "yest_yyyy = yest_yyyy - 1"
goto :done
)
rem Any other day, decrement month.
set /a "yest_mm = yest_mm - 1"
rem Need to find last day, default to 31.
set dim=31
rem Apr/Jun/Sep/Nov all have 30 days. Feb gets special handling.
if %yest_mm%==4 set dim=30
if %yest_mm%==6 set dim=30
if %yest_mm%==9 set dim=30
if %yest_mm%==11 set dim=30
if not %yest_mm%==2 goto :got_dim
rem Default Feb to 28 then use rules to override.
set dim=28
set /a "divid=yest_yyyy%%400"
if "%divid%"=="0" goto daysinmonth_29days
set /a "divid=yest_yyyy%%100"
if "%divid%"=="0" goto :done
set /a "divid=yest_yyyy%%4"
if not "%divid%"=="0" goto :done
rem Adjust to 29 days.
:daysinmonth_29days
set dim=29
:done
rem Pad out and return value.
if %yest_mm% lss 10 set yest_mm=0%yest_mm%
if %yest_dd% lss 10 set yest_dd=0%yest_dd%
set yesterday=%yest_yyyy%-%yest_mm%-%yest_dd%
endlocal && set yesterday=%yesterday%
It will set the yesterday
environment variable to the format YYYY-MM-DD
so that you can use it in your current script. Simply invoke call yesterday.cmd
and then use the environment variable.
它将yesterday
环境变量设置为格式,YYYY-MM-DD
以便您可以在当前脚本中使用它。只需调用call yesterday.cmd
然后使用环境变量即可。
回答by Martin Prikryl
It's a pretty complex task to implement with Windows batch-file and the built-in FTP client (ftp.exe
).
使用 Windows 批处理文件和内置的 FTP 客户端 ( ftp.exe
)来实现这是一项非常复杂的任务。
It would be more easier with PowerShell.
使用 PowerShell 会更容易。
And even easier using a more capable FTP client, like the latest version of WinSCP FTP client.
使用功能更强大的 FTP 客户端更容易,例如最新版本的WinSCP FTP 客户端。
If you want to download files based on a pattern in a file name, this will do:
如果要根据文件名中的模式下载文件,可以这样做:
winscp.com /ini=nul /log=yesterday.log /command ^
"open ftp://username:[email protected]/" ^
"get /remote/path/*%%TIMESTAMP-1D#yyyy-mm-dd%%* C:\local\path\" ^
"exit"
This uses the %TIMESTAMP%
syntax
If you want to download based on a file modification time, use a file mask with a time-constraint:
winscp.com /ini=nul /log=yesterday.log /command ^
"open ftp://username:[email protected]/" ^
"get /remote/path/*>=yesterday<today C:\local\path\" ^
"exit"
The >=yesterday<today
syntaxis supported by WinSCP 5.15 and newer.
WinSCP 5.15 及更新版本支持该>=yesterday<today
语法。
In older versions of WinSCP, you can again use %TIMESTAMP%
syntax, particularly >=%%TIMESTAMP-1D#yyyy-mm-dd%%<%%TIMESTAMP#yyyy-mm-dd%%
, instead of >=yesterday<today
.
在旧版本的 WinSCP 中,您可以再次使用%TIMESTAMP%
语法,尤其是>=%%TIMESTAMP-1D#yyyy-mm-dd%%<%%TIMESTAMP#yyyy-mm-dd%%
, 代替>=yesterday<today
。
(I'm the author of WinSCP)
(我是 WinSCP 的作者)
回答by AlexPace
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
这是一个示例 FTP 脚本,几乎完全符合您的需要,但它使用 3rd 方客户端,而不是 Windows 免费提供的客户端:http: //kb.robo-ftp.com/script_library/show/45
Maybe you can convert it.
也许你可以转换它。