Windows批处理脚本下载昨天的文件

发布于 2024-12-04 11:52:24 字数 575 浏览 0 评论 0原文

我正在编写一个使用 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。最后一个数字是序列号,可以是0123...

如何可以通过批处理脚本下载这些文件吗?如何修改我的脚本以下载多个文件(数量未知)哪个文件名模式是昨天?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜`诱少女 2024-12-11 11:52:24

在下载多个文件方面,使用mget而不是get。前者允许您指定获取通配符而不是特定文件。

您只需使用通配符模式构造“名称”,并确保脚本中的 mget 之前有一个 prompt,否则它将要求对每个文件。

这尚未经过测试,但可能很简单,只需将:更改

echo get %name% >> ftp.txt

为:

echo prompt>>ftp.txt
echo mget *%prefix%*>>ftp.txt

就获取昨天的日期而言,您可以使用以下脚本。与您在 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%

它将把 yesterday 环境变量设置为 YYYY-MM-DD 格式,以便您可以在当前脚本中使用它。只需调用 call Tuesday.cmd 然后使用环境变量即可。

In terms of downloading multiple files, use mget instead of get. 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 before mget otherwise it will ask for confirmation on every file.

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.

@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.

熟人话多 2024-12-11 11:52:24

使用 Windows 批处理文件和内置 FTP 客户端 (ftp.exe) 来实现这是一项相当复杂的任务。

使用 PowerShell 会更容易。


使用功能更强大的应用程序甚至更容易,例如 WinSCP FTP 客户端

如果您想根据文件名中的模式下载文件,可以这样做:

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"

这使用 %TIMESTAMP% 语法


如果您想根据文件修改时间进行下载,请使用具有时间限制的文件掩码< /a>:

winscp.com /ini=nul /log=yesterday.log /command ^
    "open ftp://username:[email protected]/" ^
    "get /remote/path/*>=yesterday<today C:\local\path\" ^
    "exit"

(我是 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:

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"

(I'm the author of WinSCP)

南…巷孤猫 2024-12-11 11:52:24

这是一个示例 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文