The FTP clients shipped with Microsoft's operating systems allow a set of command line
switches. This is the full syntax reference:
FTP switches. Syntax:
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.
We can automatize any FTP task using the -s switch. Let's learn it by sample. Suppose we
have to download a file named 'myfile.txt' from a server on a daily basis. We want this file
to be saved on C: drive at MyDocuments folder.
We start by writing a CMD or BAT file.
ftp -s:C:\batch\inst.txt ftp.server.com
The -s switch tell the client to execute the FTP commands in the text file named
C:\batch\inst.txt upon successful conection.
We can save the CMD file anywhere in our machine. It can be handy to keep it inside a directory listed in the PATH system variable.
Now we need to create the inst.txt file like this:
username password lcd .. lcd "My Documents" cd /www/main/include pwd ascii get myfile.txt quit
The first two lines are the user and password used to get into the FTP server. The third and forth lines make My Documents the default folder for this execution. The line starting with 'cd' puts us in the '/www/main/include' server directory. Then we tell the server we are going to get ascii data (It could have been 'bin' for binary data). And we quit the server after dowloading myfile.txt
After executing our batch process we are going to see somthing like this:
C:\batch>ftp -s:C:\batch\inst.txt ftp.server.com Conectado a ftp.server.com. 220 Welcome to server.com User: 331 Password required for nombreusuario. 230 User nombreusuario logged in. ftp> ftp> lcd .. Local directory now C:\. ftp> lcd c:\"my documents" Local directory now C:\My documents. ftp> cd /www/main/include 250 CWD command successful. ftp> pwd 257 "/www/main/include" is current directory. ftp> ascii 200 Type set to A. ftp> get miarchivo.txt 200 PORT command successful. 150 Opening ASCII mode data connection for miarchivo.txt (2410343 bytes). 226 Transfer complete. ftp: 2424638 bytes received in 89.31Segundos 27.15KB/s. ftp> quit 221 Goodbye.
This script was taken from robvanderwoude.com. The original was based in Tom Lavedas' Batch File Applications page.
If you've found this to be an useful article, don't forget to share it.