使用FTP下载目录,即FTP命令批量自动下载的bat文件
(编辑:jimmy 日期: 2024/11/16 浏览:3 次 )
FTP不支持目录的下载,不过FTP命令提供了mget命令,允许多文件下载,但每下载一个文件都需要确认,不能自动完成。本文主要介绍使用批处理,结合FTP的相关命令来实现批量文件的下载,以便达到目录下载的目的。
脚本内容,如
cd mydir
ls
bye
可以将mydir命令列出
然后使用批处理分析result.txt
从150 Opening ASCII mode data connection for file list.的下一行开始算,直到226 Transfer complete都是要下载的内容
然后用批处理加ftp脚本,使用get命令逐个下载
Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.
FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-w:windowsize] [-A] [host]
-v Suppresses display of remote server responses.
-n Suppresses auto-login upon initial connection.
-i Turns off interactive prompting during multiple file
transfers.
-d Enables debugging.
-g Disables filename globbing (see GLOB command).
-s:filename Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
-a Use any local interface when binding data connection.
-A login as anonymous.
-w:buffersize Overrides the default transfer buffer size of 4096.
host Specifies the host name or IP address of the remote
host to connect to.
Notes:
- mget and mput commands take y/n/q for yes/no/quit.
- Use Control-C to abort commands.
echo mget *ftp.txt
echo byeftp.txt
ftp -i -A -s:ftp.txt 10.1.103.10
echo dirftp.txt
echo byeftp.txt
ftp -A -s:ftp.txt 10.1.103.10 |find "<DIR>">result.txt
for /f "tokens=2,3*" %%i in (result.txt) do echo %%k
set host=10.1.103.10
rem FTP登入名,如果匿名不用改
set user=anonymous
rem FTP登入密码,如果匿名不用改
set pass=123456
rem 需要下载的FTP目录,如果下载子目录以分格
set dirname=目录1子目录
set homedir=%cd%
rem 生成FTP目录结构
del tree.txt
start /w cmd /c makeTree %dirname% 0
del result*.txt
for /f "delims=/" %%i in (tree.txt) do (
rem 获取dirname的所有文件
rem =============================
cd %homedir%
md %%i
cd /d %%i
echo %user%>ftp.txt
echo %pass%ftp.txt
echo cd %%iftp.txt
rem mget *.* 下载所有文件,但不能下载无扩展名的文件;
rem 如果使用mget * 可以下载所有文件,但会把子目录的文件也下载过来
echo mget *.*ftp.txt
echo byeftp.txt
ftp -i -A -s:ftp.txt %host%
del ftp.txt
rem ==============================
)
cd %homedir%
del tree.txt
rem 调用方法 maketree dir1dir2 n (n>=1)
rem %1 == dir1dir2
rem %2 == n
set /a treetime=%2+1
echo %1 tree.txt
echo %user% >ftp%treetime%.txt
echo %pass% ftp%treetime%.txt
echo cd %1 ftp%treetime%.txt
echo dir ftp%treetime%.txt
echo bye ftp%treetime%.txt
ftp -A -s:ftp%treetime%.txt %host% |find "<DIR>">result%treetime%.txt
for /f "tokens=2,3*" %%i in (result%treetime%.txt) do (start /w cmd /c Maketree.bat %1%%k %treetime%)
del result%treetime%.txt
del ftp%treetime%.txt
大致想法如下:
ftp -s:filename hosts >result.txt 执行脚本,并将结果定向输出到result.txt脚本内容,如
cd mydir
ls
bye
可以将mydir命令列出
然后使用批处理分析result.txt
从150 Opening ASCII mode data connection for file list.的下一行开始算,直到226 Transfer complete都是要下载的内容
然后用批处理加ftp脚本,使用get命令逐个下载
但实际过程中发现,在FTP脚本中使用get有问题,提示参数错误,无法使用。
查看了ftp的帮助ftp -?
G:>ftp -?Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.
FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-w:windowsize] [-A] [host]
-v Suppresses display of remote server responses.
-n Suppresses auto-login upon initial connection.
-i Turns off interactive prompting during multiple file
transfers.
-d Enables debugging.
-g Disables filename globbing (see GLOB command).
-s:filename Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
-a Use any local interface when binding data connection.
-A login as anonymous.
-w:buffersize Overrides the default transfer buffer size of 4096.
host Specifies the host name or IP address of the remote
host to connect to.
Notes:
- mget and mput commands take y/n/q for yes/no/quit.
- Use Control-C to abort commands.
发现 -i 参数可以去除mget的提示,经过测试,实现了如下脚本(使用了匿名登入,如果非匿名,可以修改登入脚本):
echo cd autoruns>ftp.txtecho mget *ftp.txt
echo byeftp.txt
ftp -i -A -s:ftp.txt 10.1.103.10
不过,这只是下载了目录中的文件,如果有子目录,那还需要用dir命令输出,并转向输出到文本,用find来提取目录,如:
echo cd autoruns>ftp.txtecho dirftp.txt
echo byeftp.txt
ftp -A -s:ftp.txt 10.1.103.10 |find "<DIR>">result.txt
for /f "tokens=2,3*" %%i in (result.txt) do echo %%k
如有子文件夹,那就要深入编写了。
经过编写测试,实现如下脚本,运行环境WINXP SP2,运行通过,未载其它系统上测试过:
1、主要脚本ftpget.bat,更改前4个变量的相关信息,该脚本调用makeTree.bat,请参见2
rem FTP服务器地址set host=10.1.103.10
rem FTP登入名,如果匿名不用改
set user=anonymous
rem FTP登入密码,如果匿名不用改
set pass=123456
rem 需要下载的FTP目录,如果下载子目录以分格
set dirname=目录1子目录
set homedir=%cd%
rem 生成FTP目录结构
del tree.txt
start /w cmd /c makeTree %dirname% 0
del result*.txt
for /f "delims=/" %%i in (tree.txt) do (
rem 获取dirname的所有文件
rem =============================
cd %homedir%
md %%i
cd /d %%i
echo %user%>ftp.txt
echo %pass%ftp.txt
echo cd %%iftp.txt
rem mget *.* 下载所有文件,但不能下载无扩展名的文件;
rem 如果使用mget * 可以下载所有文件,但会把子目录的文件也下载过来
echo mget *.*ftp.txt
echo byeftp.txt
ftp -i -A -s:ftp.txt %host%
del ftp.txt
rem ==============================
)
cd %homedir%
del tree.txt
2、maketree.bat,生成FTP目录树
rem 生成FTP目录结构rem 调用方法 maketree dir1dir2 n (n>=1)
rem %1 == dir1dir2
rem %2 == n
set /a treetime=%2+1
echo %1 tree.txt
echo %user% >ftp%treetime%.txt
echo %pass% ftp%treetime%.txt
echo cd %1 ftp%treetime%.txt
echo dir ftp%treetime%.txt
echo bye ftp%treetime%.txt
ftp -A -s:ftp%treetime%.txt %host% |find "<DIR>">result%treetime%.txt
for /f "tokens=2,3*" %%i in (result%treetime%.txt) do (start /w cmd /c Maketree.bat %1%%k %treetime%)
del result%treetime%.txt
del ftp%treetime%.txt
虽然已经实现,但仍然有小问题,就是mget *.* 不能下载无扩展名的程序;如果使用mget * 可以下载所有文件,但会把子目录的文件也下载过来。get在FTP脚本中出错也不知为何。
相关信息参考: 使用FTP 批处理脚本,Ftp 子命令
下一篇:批处理应用 临时目录清理