# How To script ftp.exe

Every version of Windows from Windows95 on has included a command line FTP client called ftp.exe. An often overlooked feature of this handy little utility 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. If you do use a shortcut, you will need to change the "%0.bat" in the first line to just "%0", so it doesn't turn "getmydata.bat" into "getmydata.bat.bat"

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.

home | resume |