Use FTP to download the directory, that is, the FTP command batch automatically downloaded BAT file _dos/bat

Source: Internet
Author: User
Tags anonymous ftp commands ftp login
FTP does not support directory downloads, but FTP commands provide the mget command, allowing multiple file downloads, but each download a file needs to be confirmed, can not be automatically completed. This article mainly introduces the use of batch processing, combined with FTP related commands to achieve batch file download, in order to achieve the purpose of directory download.

The general idea is as follows:

Ftp-s: filename hosts >result. TXT executes the script and outputs the results to result. Txt
Script content, such as
CD Mydir
Ls
Bye
You can list the Mydir commands
You then use batch processing to parse result. Txt
From the Opening ASCII mode data connection for file list. The next line starts counting, until the Transfer complete are all content to download
Then use the batch process with the FTP script, download one by one using the Get command

But the actual process found that in the FTP script to use get has a problem, the hint parameter error, cannot be used.

See the FTP Help FTP-?

G:>FTP-?

TransfersFilesTo and from a computer running a 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. TurnsOffInteractive prompting during multiple file
Transfers.
-D Enables debugging.
-G disables filename globbing(GLOBCommand).
-S: filenameSpecifies a text file containing FTP commands;The
Commands would automaticallyRunAfter FTP starts.
-AUseAny local interfaceWhenBinding 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 . /span>

I found that the-I parameter can remove mget hints and, after testing, implements the following script (using anonymous login, if not anonymous, you can modify the login script):

echo cd autoruns>ftp. Txt
echo mget *>>ftp. Txt
echo bye>>ftp. Txt
Ftp-i-a-s: FTP. TXT 10.1. 103.10

However, this only downloads the files in the directory, if there is a subdirectory, it also needs to use the dir command output, and to the output to the text, use Find to extract the directory, such as:

Echo   cd  autoruns ftp . TXT
echo   dir >> ftp . TXT
echo  bye >> ftp . TXT
ftp -a -s : FTP . TXT&NBSP 10.1 . 103.10  | Find   " <dir> " " result . TXT
for   / f  " tokens=2,3* "  %%i in  ( result . TXT )   do   echo  %%k

If you have subfolders, you need to write them down in depth.

After writing the test, implement the following script, run the environment WinXP SP2, run through, not on other systems tested:

1, the main script ftpget.bat, change the first 4 variables related information, the script calls Maketree.bat, see 2

RemFTP server address
SetHost=10.1.103.10

RemFTP login name, if anonymous do not change
SetUser=Anonymous

RemFTP login password, if anonymous do not change
SetPass=123456

RemNeed to download the FTP directory, if you download subdirectories to the grid
SetDirName=Catalog 1Sub Directory

SetHomedir=%Cd%

RemBuilding the FTP directory structure
DelTree.Txt
Start/WCmd/C Maketree%dirname%0
Delresult*.Txt

For/F"delims=/"%%i in(Tree.Txt)Todo(

RemGet all files for dirname
Rem=============================
Cd%homedir%
Md%%i
Cd/D%%i
Echo%user%>Ftp.Txt
Echo%pass%>>Ftp.Txt
Echo   cd  %%i >> ftp . TXT

rem  mget *.*  download all files, but cannot download files without extensions;
rem   If you use mget  *  can download all files, but will also download the files of the directory file
echo  mget * . * >> ftp . TXT

echo  bye >> ftp . TXT
ftp -i -a -s : FTP . txt %host%
del  ftp . TXT
rem  ==============================

)
CD  %homedir%
del   tree . TXT

2, Maketree.bat, generate the FTP directory tree

RemBuilding the FTP directory structure
RemCall method Maketree Dir1dir2 N (n>=1)
Rem%1 = = Dir1dir2
Rem%2 = = N

Set/A treetime=%+1
Echo%>>Tree.Txt

Echo%user%>Ftp%treetime%.Txt
Echo%pass%>>Ftp%treetime%.Txt
EchoCd%>>Ftp%treetime%.Txt
EchoDir>>Ftp%treetime%.Txt
EchoBye>>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 &NBSP / c maketree . bat  % 1 %%k %treetime% )
del  result%treetime% . TXT
del  ftp%treetime% . TXT

Although it has been implemented, but there is still a small problem, that is, mget *.* can not download the program without extensions, if the use of Mget * can download all files, but will be the directory files are also downloaded. Get the error in the FTP script does not know why.

Related Information reference: use ftp batch script ,ftp subcommand

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.