从实简FTP下载图片的bat脚本

/ linux / 没有评论 / 431浏览

前言

今天有个使用bat脚本下载FTP Server中的*.png图片的问题,因为目前实简FTP不支持一个下载多个文件的命令(mget),所以就采用将*.png的图片筛选出来,然后采用每次下载一张图片的方式(get)进行下载。
平时都是写shell脚本,今天第一次写bat脚本,其中有的逻辑还可以再优化一下,以后深入了解bat脚本后再回来处理吧......

更新记录

2018-01-28:已支持mget命令

测试get命令下载多图片脚本

::set variable
set ftpServer=192.168.31.195 2121
set ftpUser=ftp
set ftpPwd=ftp


:: clean some log file
del temp.txt
del log.txt
del log2.txt


::create ftp login file
echo open %ftpServer%>temp.txt
echo %ftpUser%>>temp.txt
echo %ftpPwd%>>temp.txt
echo bin>>temp.txt
echo Prompt off>>temp.txt
echo cd /Pictures/Screenshots>>temp.txt
echo ls>>temp.txt
echo quit>>temp.txt


::get file list from ftp server
ftp -s:temp.txt>log.txt



::get file name end with *.png
@echo off & setlocal EnableDelayedExpansion
set j=0
for /f "delims=""" %%i in (log.txt) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a! |findstr ".png">>log2.txt
)


::create new download ftp file
del temp.txt
echo open %ftpIp% %ftpPort%>temp.txt
echo %ftpUser%>>temp.txt
echo %ftpPwd%>>temp.txt
echo bin>>temp.txt
echo Prompt off>>temp.txt
echo cd /Pictures/Screenshots>>temp.txt
@echo off & setlocal EnableDelayedExpansion
set j=0
for /f "delims=""" %%i in (log2.txt) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo get !a!>>temp.txt
)
echo quit>>temp.txt


::start to download all *.png file
ftp -s:temp.txt>log.txt


::clean log file
del log2.txt
del log.txt
del temp.txt

测试mget命令下载多图片脚本

::set variable
set ftpServer=192.168.31.195 2121
set ftpUser=ftp
set ftpPwd=ftp


::clean some log file
del temp.txt
del log.txt


::create ftp login file
echo open %ftpServer%>temp.txt
echo %ftpUser%>>temp.txt
echo %ftpPwd%>>temp.txt
echo bin>>temp.txt
echo Prompt off>>temp.txt
::test command "mget"
echo mget save.png fuck.jpeg>>temp.txt
echo quit>>temp.txt


ftp -s:temp.txt>log.txt


::clean some log file
del log.txt
del temp.txt