批处理bat下载FTP服务器上指定天数之前的文件
程序员文章站
2022-06-24 16:10:44
@echo off
rem 指定ftp用户名
set ftpuser=asx
rem 指定ftp密码
set ftppass=asx-asx
rem 指定...
@echo off rem 指定ftp用户名 set ftpuser=asx rem 指定ftp密码 set ftppass=asx-asx rem 指定ftp服务器地址 set ftpip=172.19.103.7 rem 指定待下载的文件位于ftp服务器的什么位置 set ftpfolder=/export/home/prod/ntcdata rem 指定从ftp下载下来的文件存放到什么地方 set localfolder=e:/temp rem 指定下载几天前的文件 set daysago=1 set ftpdir=%temp%/tempdir.txt set ftpdown=%temp%/tempdown.txt rem 假设本机日期格式为yyyy-mm-dd set filey=%date:~0,4% call :datetodays %date:~0,4% %date:~5,2% %date:~8,2% passdays set /a passdays-=daysago call :daystodate %passdays% dstyear dstmonth dstday >"%ftpdir%" echo,%ftpuser% >>"%ftpdir%" echo,%ftppass% >>"%ftpdir%" echo bin >>"%ftpdir%" echo cd "%ftpfolder%" >>"%ftpdir%" echo dir * "%temp%/filelist.txt" >>"%ftpdir%" echo bye start ftp -v -i -s:"%ftpdir%" %ftpip% rem 根据实际情况自行调节延迟时间 ping -n 200 127.1 >nul findstr /i ":*/.swm$" "%temp%/filelist.txt">"%temp%/filedate.txt" findstr /i ":*/.twm$" "%temp%/filelist.txt">>"%temp%/filedate.txt" findstr /i ":*/.owm$" "%temp%/filelist.txt">>"%temp%/filedate.txt" >"%ftpdown%" echo.%ftpuser% >>"%ftpdown%" echo.%ftppass% >>"%ftpdown%" echo bin >>"%ftpdown%" echo cd "%ftpfolder%" (for /f "usebackq tokens=1-9 delims= " %%a in ("%temp%/filedate.txt") do ( setlocal enabledelayedexpansion if "%%f" equ "jan" ( set filem=01 ) else if "%%f" equ "feb" ( set filem=02 ) else if "%%f" equ "mar" ( set filem=03 ) else if "%%f" equ "apr" ( set filem=04 ) else if "%%f" equ "may" ( set filem=05 ) else if "%%f" equ "jun" ( set filem=06 ) else if "%%f" equ "jul" ( set filem=07 ) else if "%%f" equ "aug" ( set filem=08 ) else if "%%f" equ "sep" ( set filem=09 ) else if "%%f" equ "oct" ( set filem=10 ) else if "%%f" equ "nov" ( set filem=11 ) else if "%%f" equ "dec" ( set filem=12 ) set filed=10%%g set filed=!filed:~-2! if "!filem!" equ "%dstmonth%" ( if "!filed!" equ "%dstday%" ( echo get "%%i" "%localfolder%/%%i" ) ) endlocal ))>>"%ftpdown%" >>"%ftpdown%" echo bye start ftp -v -i -s:"%ftpdown%" %ftpip% goto :eof :datetodays %yy% %mm% %dd% days setlocal enableextensions set yy=%1&set mm=%2&set dd=%3 if 1%yy% lss 200 if 1%yy% lss 170 (set yy=20%yy%) else (set yy=19%yy%) set /a dd=100%dd%%%100,mm=100%mm%%%100 set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2 set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633 endlocal&set %4=%j%&goto :eof :daystodate %days% yy mm dd setlocal enableextensions set /a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5 set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10 (if %mm% lss 10 set mm=0%mm%)&(if %dd% lss 10 set dd=0%dd%) endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%&goto :eof