批处理文件中的动态每日日期

时间:2015-01-24 06:26:03

标签: batch-file ftp

在下面的批处理文件中,日期为2015_01_21的是在ftp上按日期命名的文件夹,而csv文件的名称中也有日期

如果我必须每天通过任务计划程序运行此批处理文件,如何自动更改?

ftp.exe -s:%0   
open 111.11.111.11   
Username  
Password

get Samle/2015_01_21/Samle_AB20150121.csv.gz  
get Samle/2015_01_21_ABCD/Samle_AB210115.csv.gz

quit

2 个答案:

答案 0 :(得分:0)

有关详情,请查看this

@echo off
pushd "%temp%"
makecab /D RptFileName=~.rpt /D InfFileName=~.inf /f nul >nul
for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do (
   set "current-date=%%e-%%b-%%c"
   set "current-time=%%d"
   set "weekday=%%a"
)
del ~.*
popd
for /f "tokens=1,2,3 delims=-" %%a in ("%current-date% ") do (
    set year=%%a
    set mon=%%b
    set day=%%c
)



if /i %mon% equ jan set mon=01
if /i %mon% equ feb set mon=02
if /i %mon% equ mar set mon=03
if /i %mon% equ apr set mon=04
if /i %mon% equ may set mon=05
if /i %mon% equ jun set mon=06
if /i %mon% equ jul set mon=07
if /i %mon% equ aug set mon=08
if /i %mon% equ sep set mon=09
if /i %mon% equ oct set mon=10
if /i %mon% equ nov set mon=11
if /i %mon% equ dec set mon=12

echo %mon% %day% %year%

break>temp.ftp
(
  echo open 111.11.111.11 
  echo username
  echo password

  rem ftp.exe supports wildcards so you can change AB to *
  rem echo get Samle/%year%_%mon%_%day%/Samle_*%year%%mon%%day%.csv.gz
  rem echo get Samle/%year%_%mon%_%day%_*/Samle_*%year%%mon%%day%.csv.gz

  rem if AB and ABCD strings are possible to change use the two lines above
  echo get Samle/%year%_%mon%_%day%/Samle_*%year%%mon%%day%.csv.gz
  echo get Samle/%year%_%mon%_%day%_ABCD/Samle_AB%year%%mon%%day%.csv.gz
)>>temp.ftp

ftp -s:temp.ftp
del /q /f temp.ftp

答案 1 :(得分:0)

要使用日期动态,你最好将临时文件传递给ftp而不是传递bat本身。试试这个。

@echo off
setlocal
for /f "skip=1 tokens=1-3" %%a in ('WMIC PATH win32_localtime get day^,month^,year') do  (
    if "%%a" neq "" (
        set day=%a
        set month=%b
        set year=%c
    )
)

set ftpfile=%tmp%\ftpfile.txt

echo open 111.11.111.11>%ftpfile%
echo Username>>%ftpfile%
echo Password>>%ftpfile%

echo get Samle/%year%_%month%_%day%/Samle_AB%year%%month%%day%.csv.gz>>%ftpfile%
echo get Samle/%year%_%month%_%day%_ABCD/Samle_AB%day%%month%%year:~-2%.csv.gz>>%ftpfile%

echo quit>>%ftpfile%

ftp.exe -s:%ftpfile%
del %ftpfile%