编写DOS批处理脚本从FTP下载文件

工作当中经常需要从远程Linux的指定目录下载文件到自己日常使用的Windows系统,老是通过FTP工具比较麻烦,所以写个batch脚本,每次需要下载的时候只要跑一下脚本就行了,非常方便。

PS:脚本中有些代码“没用”,其实是为了考虑后期扩展

单个batch脚本,从ftp下载文件

@echo off
SETLOCAL

set BASE_PATH=%~dp0
cd %BASE_PATH%

echo open 192.168.1.6 >%BASE_PATH%\get_yunzhu.txt
echo user yunzhu yunzhu >>%BASE_PATH%\get_yunzhu.txt
echo cd /home/yunzhu >>%BASE_PATH%\get_yunzhu.txt
echo prompt off >>%BASE_PATH%\get_yunzhu.txt
echo mget yunzhu.* >>%BASE_PATH%\get_yunzhu.txt
echo bye >>%BASE_PATH%\get_yunzhu.txt
echo quit >>%BASE_PATH%\get_yunzhu.txt
echo exit >>%BASE_PATH%\get_yunzhu.txt

ftp -n -s:%BASE_PATH%\get_yunzhu.txt

del %BASE_PATH%\get_yunzhu.txt

EXIT /b 0

batch 脚本,根据配置文件,从ftp下载文件

@echo off
SETLOCAL

set BASE_PATH=%~dp0
cd %BASE_PATH%

set CFG_FILE=%BASE_PATH%\getfile.ini
set LOG_FILE=%BASE_PATH%\getfile.log
set SAVE_PATH=%BASE_PATH%\download
set TMP_PATH=%BASE_PATH%\temp

echo ======================================
set datetime=%date%%time%
echo Start time : %datetime%
PAUSE

if not exist %BASE_PATH% (
	mkdir %BASE_PATH%
)
if not exist %SAVE_PATH% (
	mkdir %SAVE_PATH%
)
if not exist %TMP_PATH% (
	mkdir %TMP_PATH%
)

del /Q %SAVE_PATH%\*
del /Q %TMP_PATH%\*

cd %TMP_PATH%

ftp -n -s:"%CFG_FILE%"

move /Y %TMP_PATH%\* %SAVE_PATH%\

echo --------------------------------------
del %BASE_PATH%\filelist.tmp
dir /B %SAVE_PATH% >> %BASE_PATH%\filelist.tmp
PAUSE
FOR /F "usebackq delims=" %%i IN (`type %BASE_PATH%\filelist.tmp`) DO call echo %%i

echo --------------------------------------
set datetime=%date%%time%
echo End time : %datetime%
PAUSE

ENDLOCAL
EXIT /b 0

上面 batch 脚本使用的配置文件:getfile.ini

open 192.168.1.6
user yunzhu yunzhu
cd /home/yunzhu
prompt off
mget *.*
bye
quit
exit