tinyapps.org / docs / Scripting FTP.exe


The following article was kindly submitted by William Kitchen.

An often overlooked feature of the Windows command line ftp program is that it can be scripted. The script is simply a text file containing exactly what you would have typed if using it interactively.

For example, if you create a text file called "updatewebsite" containing something similar to this:

lcd \mylocalwebpages
open someftpserver.somedomain.org
myusername
mypassword
cd httpd/docs
prompt
mput *.html
binary
mput *.jpg
mput *.png
bye

you can then update your web site by typing this at the command line:

ftp -s:updatewebsite

While browsing the web I stumbled upon an excellent web page about batch programming that describes a clever way to embed an ftp script in a batch file that passes itself to the ftp program as the script.

If you're familiar with batch files, you probably already know that a batch file can reference command line parameters using %1 for the first parameter, %2 for the second, etc. But less widely known is that %0 is also valid, and represents the item before the first parameter on the command line, which is, of course, the command itself.

If you create a file called getmydata.bat containing this:

ftp -s:%0.bat
goto done
open someftpserver.somedomain.org
myusername
mypassword
binary
get mydata.dat
bye
:done

you could then retrieve "mydata.dat" from the remote system just by typing "getmydata" at a command line. If it's something you do a lot, you could make it even easier by placing a shortcut on your desktop or quicklaunch toolbar.

The ftp script is effectively hidden from the batch interpreter because the second line causes the batch interpreter to skip over it. You can't hide the first two batch commands from the ftp program, but the script works anyway because the ftp program simply generates an error message and skips to the next line whenever it encounters something it doesn't understand. Any batch commands added after the ":done" label will not be seen by the ftp program because it exits when it gets to the "bye" at the end of the script.


posted: 2001.10.27