Windows批处理脚本下载文件昨天 [英] Windows batch script to download yesterday files

查看:162
本文介绍了Windows批处理脚本下载文件昨天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写使用 Ftp.exe的从FTP服务器下载文件的脚本,它工作在第一。但我写的版本适用于只有一个文件和当前日期。我的脚本如下:

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

但现在有这样命名 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 ...

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?

推荐答案

在下载多个文件,请使用方面 MGET 而不是 GET 。前者可以让你获取,而不是特定的文件指定通配符。

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 ,否则它会询问每个文件确认。

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

喜欢的东西:

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


在越来越昨日的方面,可以使用下面的脚本。这是pretty复杂相比,你会做什么,例如庆典,但它的作品。

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

这将在昨日环境变量设置为格式 YYYY-MM-DD ,这样就可以使用它在当前的脚本。简单地调用呼叫yesterday.cmd ,然后使用环境变量。

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.

这篇关于Windows批处理脚本下载文件昨天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆